Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS triangle :before element

I'm using bootstrap, trying to make a div have a CSS triangle before it.

http://jsfiddle.net/B2XvZ/11/

Here is my non-working code:

.d:before {   width: 0px;   height: 0px;   border-style: solid;   border-width: 10px 15px 10px 0;   border-color: transparent #dd4397 transparent transparent;   } 

The way I'd like it to look is for there to be a pink left-pointing triangle right before the text "this", with no gap between it and the div. I've tried to do this by floating the elements also, with no success.

like image 747
amagumori Avatar asked Dec 15 '13 00:12

amagumori


People also ask

How do I make a triangle in CSS before?

Steps to create a basic triangleborder-top. border-bottom. border-left. border-right.

What is use of before and after in CSS?

Definition and UsageThe ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.

How do you convert a square to a triangle in CSS?

By setting the width and height to zero on the element, the actual width of the element is going to be the width of the border. Keep in mind that the border edges on an element are 45 degree diagonals to each other. That's why this method works to create a triangle.


2 Answers

You need to specify the content property.

For positioning, add position:relative to the parent, and then absolutely position the arrow -15px to the left.

jsFiddle example

.d {     position:relative; }  .d:before {     content:"\A";     border-style: solid;     border-width: 10px 15px 10px 0;     border-color: transparent #dd4397 transparent transparent;     position: absolute;     left: -15px; } 
like image 81
Josh Crozier Avatar answered Oct 08 '22 08:10

Josh Crozier


You need content property and some other

.d:before {   content: '';   width: 0px;   height: 0px;   border-style: solid;   border-width: 10px 15px 10px 0;   border-color: transparent #dd4397 transparent transparent;     display: inline-block;   vertical-align: middle;   margin-right: 5px; } 
like image 37
Slawa Eremin Avatar answered Oct 08 '22 09:10

Slawa Eremin