Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

width and height doesn't seem to work on :before pseudo-element

Here is a fiddle.

<p>foo <a class="infolink" href="#">bar</a> baz</p> 

and

a.infolink::before {     content: '?';     background: blue;     color: white;     width: 20ex;     height: 20ex; } 

The '?' appears but clearly does not have 20ex size. Why not? Tested in Firefox and Chrome.

like image 547
spraff Avatar asked Dec 31 '13 14:12

spraff


People also ask

Why height and width is not working in CSS?

While the display property is inline , height and width will not work. display :inline; By changing the display property from inline to block or inline-block , height and width should be worked properly.

Which element height and width can not be set?

Inline element properties: The height of an inline element is the height of the content. The width of an inline element is the width of the content. The height and width of an inline element cannot be set in CSS.

What does the pseudo element :: Before do?

::before (:before) In CSS, ::before creates a pseudo-element that is the first 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.

Does Z Index work on pseudo-elements?

It is important to realize that pseudo-elements are considered descendants of their associated element. You may set a negative z-index for these pseudo-elements, but in order for them to actually appear below their parent element, you must create a new stacking context for the parent.


1 Answers

Note: The ::before and ::after pseudo-elements are actually laid display: inline; by default.

Change the display value to inline-block for the width & height to take effect while maintaining inline formatting context.

a.infolink::before {     content: '?';     display: inline-block;     background: blue;     color: white;     width: 20px;     height: 20px; } 

http://jsfiddle.net/C7rSa/3/

like image 198
Adrift Avatar answered Sep 18 '22 14:09

Adrift