Universal Selector

Term side of the Card.
Topic: Selectors
Unit title: Universal Selector
Categories: Selector
Explanation side of the Card

Explanation: The selector that targets all elements within a document. Represented by the '*' symbol, it applies styles to every element, useful for applying global styles or resetting default browser styling.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 1.
Availability: 

Example

Wrongcss
* {
  box-sizing: border-box;
}
Rightcss
*,
*::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-size on * breaks inheritance for nested em values.

Key points

  • The box-sizing reset includes *::before and *::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?

No, the universal selector has zero specificity, so any other selector beats it.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy