Universal Selector
Example
* {
box-sizing: border-box;
}*,
*::before,
*::after {
box-sizing: border-box;
}* does not match pseudo-elements, so they need to be listed for the reset to cover them.
Common mistakes
- Setting
* { margin: 0 }and then wondering why lists and headings lose spacing. - Setting
font-sizeon*breaks inheritance for nestedemvalues.
Key points
- The box-sizing reset includes
*::beforeand*::after. *is used only in small resets, not to set fonts or colors on everything.
Interview questions
Is the universal selector slow?
Not in practice. Modern engines match it quickly, though deep chains like .a * .b can still do extra work.
Does * add specificity?
* add specificity?No, the universal selector has zero specificity, so any other selector beats it.