Sub topic 3
The universal selector
This chapter assumes you can write type, class, and ID selectors and read a specificity triple written as (IDs, classes, types). Here you will see what * matches, why it adds nothing to specificity, and where it causes surprises.
What `*` matches
The universal selector is written *. It matches every element. Used alone, it applies a declaration to the whole page:
* {
box-sizing: border-box;
}Placed next to another selector, it limits the match to elements in that position:
.card * { margin: 0; }
.card > * { padding: 1rem; }
* + * { margin-top: 1rem; }.card *matches every descendant of.card, at any depth..card > *matches only the direct children of.card.* + *matches any element that directly follows a sibling. This is a common way to space out a stack of elements without touching the first one.
You can drop the * when it sits in front of a class, ID, attribute, or pseudo-class. *.note and .note are the same selector, as are *:hover and :hover. Most authors leave it out.
Specificity of `*`
The universal selector contributes (0,0,0). Combinators contribute nothing either, so only the other parts of the selector count:
* { color: red; } /* (0,0,0) */
p { color: black; } /* (0,0,1) */
.card * { margin: 0; } /* (0,1,0) */
.card > * { padding: 1rem; } /* (0,1,0) */Any rule with at least one type, class, or ID selector beats a bare * on the same element and property. This makes * a good place for defaults that you expect to be overridden.
Pseudo-elements
* does not match pseudo-elements. If you want a rule to reach generated content, you must name it:
*,
*::before,
*::after {
box-sizing: border-box;
}Each selector in the list is evaluated on its own, so the list is equivalent to three separate rules.
Common errors
Expecting * to lose to inheritance. A rule with zero specificity still beats an inherited value. Inheritance only supplies a value when no rule targets the element directly. * targets every element directly, so it replaces the value inherited from the parent:
body { color: navy; }
* { color: gray; }Here the body text is gray, not navy. Every element, including body itself, matches *. A paragraph inside the body also matches *, so it gets gray directly and ignores the navy it would have inherited. For properties that inherit, such as color, font-family, and line-height, a broad * rule can defeat your intended cascade of values from ancestors.
Using * as a reset that is hard to undo. A rule such as * { margin: 0; } has zero specificity, so it is easy to override. A rule such as .sidebar * { margin: 0; } has (0,1,0), and overriding it for one element inside .sidebar requires a selector at least as specific, placed later.
Assuming * is slow. Modern browsers match it efficiently. A bare * rule is rarely a performance problem. Descendant selectors that end in *, applied across a very large page, are worth measuring if you suspect a problem.
Forgetting * skips the shadow DOM. A document-level * rule does not reach inside a shadow root. Elements in a shadow tree need their own styles, or the component must expose inheritable properties.
Exercise
A page has body { color: navy; } and * { color: gray; }. A <p> inside the body has no other styles. What color is its text, and why?
Show answer
Gray. The * rule matches the <p> directly, so it sets the color on the element itself. Specificity is only compared between rules that target the element, and inheritance supplies a value only when no rule does. The navy on body would be inherited, so it loses even to a rule with specificity (0,0,0).