Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS for ribbon end

How do I draw the end of a ribbon like the left side of this image using only CSS? enter image description here

I know I can use the fact that corners in CSS are mitered, so I can have a div with a border of size 0 and other borders bigger to give me triangles. Is there a way to do this with only 1 div? Or do I need to stack some triangles? I'd really prefer to have 1 div so that users don't have to think about this and I can just use the CSS :before pseudo element to insert this. What's the best way to implement this?

IE9+ and modern versions of other browsers only need to be supported.

like image 590
at. Avatar asked Nov 12 '12 09:11

at.


1 Answers

HTML

<div class="ribbon">
   <strong class="ribbon-content">Everybody loves ribbons</strong>
</div>​

CSS

    .ribbon {
     font-size: 16px !important;
     width: 50%;
     position: relative;
     background: #ba89b6;
     color: #fff;
     text-align: center;
     padding: 1em 2em; /* Adjust to suit */
     margin: 2em auto 3em; 
    }
    .ribbon:before {
     content: "";
     position: absolute;
     display: block;
     bottom: -1em;
     border: 1.5em solid #986794;
     z-index: -1;
    }
    .ribbon:before {
     left: -2em;
     border-right-width: 1.5em;
     border-left-color: transparent;
    }

.ribbon .ribbon-content:before {
 content: "";
 position: absolute;
 display: block;
 border-style: solid;
 border-color: #804f7c transparent transparent transparent;
 bottom: -1em;
}
.ribbon .ribbon-content:before {
 left: 0;
 border-width: 1em 0 0 1em;
}

See Demo

Reference

like image 185
Priyank Patel Avatar answered Sep 29 '22 14:09

Priyank Patel