Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position a CSS triangle using ::after?

I've created a div with a bottom arrow using ::after. Here is the HTML code:

<div class="sidebar-resources-categories">Topics</div> <div class="text-content">ok ok</div> 

And here is the CSS:

.sidebar-resources-categories{     height: 50px;     margin-bottom: 20px;     background-color: #e8e8e8;     font-weight: 600;     line-height: 50px;     text-align: center;     font-size: 20px; } .sidebar-resources-categories::after{     content: '';     position: absolute;     left: 42%;     top: 100%;     width: 0;     height: 0;     border-left: 20px solid transparent;     border-right: 20px solid transparent;     border-top: 20px solid #e8e8e8;     clear: both; } 

Here is the result:

Here is the result

I would like the arrow to be at the very bottom of the grey div. I don't want to have the content between the div and the bottom arrow. Do you know how I can do that?

like image 998
Damien Morvan Avatar asked Jul 31 '14 17:07

Damien Morvan


People also ask

What does :: after in CSS mean?

::after (:after) In CSS, ::after creates a pseudo-element that is the last child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

How do I change the position of a shape in CSS?

Fixed Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.

What is :: after and :: before 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. Version: CSS2.


2 Answers

Just add position:relative to the parent element .sidebar-resources-categories

http://jsfiddle.net/matthewabrman/5msuY/

explanation: the ::after elements position is based off of it's parent, in your example you probably had a parent element of the .sidebar-res... which had a set height, therefore it rendered just below it. Adding position relative to the .sidebar-res... makes the after elements move to 100% of it's parent which now becomes the .sidebar-res... because it's position is set to relative. I'm not sure how to explain it but it's expected behaviour.

read more on the subject: http://css-tricks.com/absolute-positioning-inside-relative-positioning/

like image 180
Matthew Abrman Avatar answered Oct 04 '22 14:10

Matthew Abrman


Add a class:

.com_box:after {      content: '';     position: absolute;     left: 18px;     top: 50px;     width: 0;     height: 0;     border-left: 20px solid transparent;     border-right: 20px solid transparent;     border-top: 20px solid #000;     clear: both;  } 

Updated your jsfiddle: http://jsfiddle.net/wrm4y8k6/8/

like image 37
Elow Avatar answered Oct 04 '22 15:10

Elow