Purpose of *:before, *:after rule without content property
Asked Answered
C

3

7

From what I understand, :before and :after inserts content before or after the indicated target. I'm not really sure what would be the purpose of this CSS snippet?

*, *:before, *:after {
    -moz-box-sizing: border-box;
}
Commie answered 8/5, 2014 at 13:3 Comment(7)
It adds to everything border-box so the width of the element takes into consideration padding and border. In this context i guess you took it from a css reset right ?Cockroach
With that code in the width you specify to elements padding and border is included as well.Kentkenta
@PatsyIssa But it makes no sense here. the * adds this to all elements. So there is no need to :before or :afterInsolation
@Insolation True. I can only see the option that the writer wanted to really make sure the style is applied to everything (even though only the firt selector was needed).Kentkenta
Before and after don't inherit box-sizing from the parentCockroach
@PatsyIssa you should make an answer of your comments :)Childbirth
@GCyrillus It's a trivial question and it already has 3 answers that state the same thing, no point ^^.Cockroach
C
7

That applies border-box sizing to all elements as well as any :before and :after pseudo-elements that they may generate. The *:before, *:after portion means the respective pseudo-elements of any element.

Once you create specific :before/:after rules later in your stylesheet, this declaration will apply automatically to all of those pseudo-elements, so you don't have to repeat it in every single one of your pseudo-element rules. In other words, the cascade works exactly the same way for pseudo-elements as it does with actual elements: when you have separate rules matching the same thing, as long as they match, they will all be applied.

Note that in order for an element to actually generate a :before or :after, its content must be something other than none. By itself, the CSS that you have given will not cause every element to automatically generate both pseudo-elements; it just ensures the browser will use border-box sizing if it does need to render any of them. See the spec for how generated content works.

For example, the following CSS:

*, *:before, *:after {
    box-sizing: border-box;
}

div:after {
    content: "hello";
}

results in a div's :after pseudo-element having border-box sizing. No other elements generate :after pseudo-elements, but should more CSS rules be introduced they will all have the same box-sizing from the universal rule.

Note also that box-sizing: border-box without the -moz- prefix should appear in the given CSS so other browsers will apply the same box sizing as well. The -moz- prefix is used by Firefox up to version 28 (the just-released version 29 ships with unprefixed box-sizing). See this answer.

Changeling answered 8/5, 2014 at 13:8 Comment(0)
C
0

It applies to all the elements with all the pseudo elements (:before, :after), and it makes them use border-box box sizing.

For more information about box-sizing check here: http://css-tricks.com/box-sizing/

The border-box value makes the final rendered box the declared width, and any border and padding cut inside the box.

Calendula answered 8/5, 2014 at 13:7 Comment(0)
A
-2

*, *:before, *:after { box-sizing: border-box; }

This is for not to repeat in all places where you use :after and :before. It automatically adds to all :after and :before.

Agree answered 7/9, 2019 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.