NewToJS
NewToJS

Reputation: 2101

Is there a way to make this ribbon a pure css ribbon? aka: can I just add a ribbon-updated class

I have this div where I add an orange 'ribbon' with the text: UPDATED to the top right corner like so: enter image description here

<div class='portfolio-item' >
  <div class='ribbon-updated'>
    <p>updated</p>
  </div>
</div>

basically .portfolio-item has the css (along with some other css not relevant here):

position: relative 
overflow: hidden

And the css for .ribbon-updated

   .ribbon-updated
        position: absolute
        background-color: $orange-medium
        transform: rotate(45deg)
        padding: 5px 10px
        top: 15px
        right: -35px
        left: auto
        width: 130px
        p
            color: $white
            margin-bottom: 0
            text-transform: uppercase
            letter-spacing: 1px
            font-weight: 600
            text-align: center

But is there a way to do this by not adding a whole new div.ribbon-updated? Could I just add a css class like 'ribbon' to div.portfolio-item to give it a ribbon?

Upvotes: 2

Views: 192

Answers (1)

cyr_x
cyr_x

Reputation: 14257

Sure it's possible with just CSS, using the pseudo classes before or after. You could even read the ribbon text value from a html attribute:

.portfolio-item {
  width: 200px;
  height: 300px;
  background: #ddd;
  position: relative;
  overflow: hidden;
  float: left;
  margin: 10px;
}

.portfolio-item[data-ribbon]::before {
  content: attr(data-ribbon);
  position: absolute;
  background-color: orange;
  transform: rotate(45deg);
  line-height: 32px;
  padding: 0 10px;
  top: 24px;
  right: -35px;
  width: 130px;
  color: white;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-weight: 600;
  text-align: center;
}
<div class='portfolio-item' data-ribbon="updated">
</div>
<div class='portfolio-item' data-ribbon="custom">
</div>
<div class='portfolio-item' data-ribbon="new">
</div>

Browser support for CSS generated content can be found here.

Upvotes: 4

Related Questions