Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the :before and :after pseudo-elements inherit height from the parent element?

I am wondering whether the :before and :after pseudo-elements can inherit the height from parent using the inherit value, without the actual element doing so?

like image 572
AKG Avatar asked Aug 25 '12 19:08

AKG


People also ask

What is before and after pseudo-elements?

CSS ::before and ::after pseudo-elements allow you to insert “content” before and after any non-replaced element (e.g. they work on a <div> but not an <input> ). This effectively allows you to show something on a web page that might not be present in the HTML content.

What does the before pseudo-element do?

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.

Can I have multiple before pseudo-elements for the same element?

In CSS2. 1, an element can only have at most one of any kind of pseudo-element at any time. (This means an element can have both a :before and an :after pseudo-element — it just cannot have more than one of each kind.)

Can pseudo-elements and pseudo-classes Cannot be combined?

There are no special rules around combining pseudo-classes and pseudo-elements, besides the one rule that says there can only be one pseudo-element per complex selector and it must appear at the very end.


1 Answers

No. The only way that pseudo-elements can inherit values from the parent of their generating element is when the generating element itself is also inheriting from its parent.

This is because inheritance occurs from a parent to a child, one level at a time. For inheritance to work across several levels of descendants, every descendant must inherit.

As an example, consider the following HTML:

<div class="parent">     <div class="child">     </div> </div> 

With the following CSS:

.parent {     width: 100px;     height: 100px; }  .parent > .child:before, .parent > .child:after {     content: '';     position: absolute;     width: inherit;     height: inherit; } 

This will not work because even though the pseudo-elements have values of inherit, the element generating them, that is, .parent > .child, does not inherit from .parent. Instead, they inherit the default value of auto for both properties.

In order for this to work you will need to have .parent > .child inherit as well:

.parent {     width: 100px;     height: 100px; }  .parent > .child {     width: inherit;     height: inherit; }  .parent > .child:before, .parent > .child:after {     content: '';     position: absolute;     width: inherit;     height: inherit; } 
like image 181
BoltClock Avatar answered Sep 22 '22 23:09

BoltClock