You can create post and price tags using CSS3 and place them into your blog, ecommerce website or any other kind of website. This is a rather simple CSS trick which uses :before
and :after
pseudo-elements to draw triangle and circle in post and price tags we are creating here.
Post and price tags using CSS3
I am using tags
class to apply common CSS rules to both type of tags along with :before
and :after
pseudo-elements as well as price-tag
and post-tag
classes to achieve desired styling (triangle/circle on the left or right).
HTML
It’s quite simple.
1 2 3 4 | <span class="tags"> <span class="price-tag"><a href="javascript:void()">$100</a></span> <span class="post-tag"><a href="javascript:void()">Sample Text</a></span> </span> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | .tags a { float: left; position: relative; width: auto; height: 30px; margin-left: 20px; padding: 0 12px; line-height: 30px; background: #1f8dd6; color: #fff; font-size: 18px; font-weight: 600; text-decoration: none; } .tags a:before { content: ""; position: absolute; top: 0; width: 0; height: 0; border-style: solid; } .tags a:after { content: ""; position: absolute; top: 13px; width: 4px; height: 4px; -moz-border-radius: 2px; -webkit-border-radius: 2px; border-radius: 2px; background: #fff; -moz-box-shadow: -1px -1px 2px #004977; -webkit-box-shadow: -1px -1px 2px #004977; box-shadow: -1px -1px 2px #004977; } |
- .tags a – We are defining CSS rules for anchor styling.
- .tags a:before – We have applied zero top edge to absolute positioned
:before
pseudo-element and width, height set to zero and a solid border style. Later we will supply left/right positioning and border width & color depending upon where we wish to triangle appear. - .tags a:after – This will act as that rounded hole. What we’re doing here is creating an empty square, and rounding it’s edges so we create a circle with
position: absolute
and of course we will position it on either left or right later in this article.
Here are some points that will sure help you to understand the concept and modify the rules to style the tags in your own way:
- The height and line-height of anchor tag should be same to vertical align the text inside tags properly and you can increase/decrease the size using these two.
- The left/right position and three sided border of triangle with
:before
pseudo-element are always equal to half of the height (mentioned inprice-tag a:before
pseudo-element later). - Top edge of hole = (height of tag – height of hole) / 2
- Left/right edge of hole = (-) width of hole / 2
Price Tags
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* Add rounded corners to right end of the anchor tag */ .price-tag a { -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 4px; border-bottom-right-radius: 4px; -moz-border-radius-topright: 4px; -webkit-border-top-right-radius: 4px; border-top-right-radius: 4px; } /* Position left and show only right border of triangle */ .price-tag a:before { left: -15px; border-color: transparent #1f8dd6 transparent transparent; border-width: 15px 15px 15px 0; } /* Fix the circle between anchor box and triangle left to it */ .price-tag a:after { left: -2px; } |
You can change the radius of border and to give top-right and bottom-right corner sharp look, comment out the first CSS rule in the code just above.
Post Tags
Same as the above we are adding post-tag
class to have circle and triangle appear to the right side of the anchor tag box.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* Add rounded corners to left end of the anchor tag */ .post-tag a { -moz-border-radius-bottomleft: 4px; -webkit-border-bottom-left-radius: 4px; border-bottom-left-radius: 4px; -moz-border-radius-topleft: 4px; -webkit-border-top-left-radius: 4px; border-top-left-radius: 4px; } /* Position right and show only left border of triangle */ .post-tag a:before { right: -15px; border-color: transparent transparent transparent #1f8dd6; border-width: 15px 0 15px 15px; } /* Fix the circle between anchor box and triangle right to it */ .post-tag a:after { right: -2px; } |
And in the last as in our DEMO we are applying hover color on tags so add the following CSS:
1 2 3 4 5 6 7 8 9 10 11 | .tags a:hover { background: #1d85ca; } .price-tag a:hover:before { border-color: transparent #1d85ca transparent transparent; } .post-tag a:hover:before { border-color: transparent transparent transparent #1d85ca; } |
In this way you can create left pointing or right pointing price tags as well as I tried to clear the concept behind post and price tags using CSS3 so that you can alter the CSS to obtain desired styling and size.