Sub topic 7
Writing defaults that are easy to override
This chapter assumes you know how specificity is calculated as an (ID, class, type) triple. It also assumes you know that :where() matches the same elements as :is() but adds nothing to specificity. Here you will use that property to write base styles that other rules can override without a fight.
The problem with strong defaults
A default style is a rule that sets a baseline, such as list spacing or link color. Later, a component needs something different. If the default has high specificity, the component rule must be at least as specific. If it isn't, the default wins even when the component rule comes later in the file.
Authors often respond by adding classes, nesting selectors deeper, or reaching for !important. Each of these makes the stylesheet harder to change.
Using :where() to zero out specificity
Wrapping the selector in :where() gives the rule a specificity of (0,0,0). Any other selector with at least one type, class or ID will beat it, whatever the source order.
:where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}Here :where(ul, ol) has specificity (0,0,0). The .menu rule has (0,1,0), so padding-left: 0 wins on any list with that class. That holds even if the .menu rule appears before the default in the file.
Compare the same default written without :where():
ul,
ol {
margin: 0;
padding-left: 1.5rem;
}This has specificity (0,0,1). The .menu rule still wins here because a class outranks a type. The difference shows up against another type selector, such as nav ul, which has (0,0,2) and wins. A source-order tie, such as a later plain ul rule, is also a difference. With :where(), the default loses to both cases.
Keeping the context part cheap too
You can put the whole context inside :where(), not only the element. Say you want default link styling for article content:
:where(article, .prose) :where(a) {
color: royalblue;
text-decoration: underline;
}The whole selector has specificity (0,0,0). Compare this with :is(article, .prose) a, which has (0,1,1) because .prose is the most specific argument in the list. A component rule such as .button would still beat it, but a rule like a:hover would not necessarily. The :where() version is easier to predict because it never competes.
What to put inside :where()
Decide which part of the selector should be allowed to be overridden. A typical pattern is to keep it narrow:
- Wrap the part that describes the default context, such as the elements or a wrapper class.
- Leave state selectors outside, such as
:hoveror:focus-visible, if you want them to keep their normal weight.
:where(a) {
color: royalblue;
}
:where(a):focus-visible {
outline: 2px solid currentColor;
}The first rule is (0,0,0). The second is (0,1,0), because :focus-visible is an ordinary pseudo-class. The focus outline therefore still beats most simple base rules, while the color stays easy to change.
Cautions
- A zero-specificity rule loses to every other rule that sets the same property on the same element. That includes unrelated rules you did not write, such as a rule from a library. Check where a default is being overridden before you assume your code is wrong.
- The universal selector
*also has no specificity, so:where(*)is not more powerful than*. It only matters when you pair it with other parts. - Zero specificity does not remove a rule from the cascade. If no other rule sets the property, the default applies normally.
Summary of the pattern
Exercise
A stylesheet has :where(.card) p { margin: 0; } followed by p { margin: 1rem; }. Which margin does a paragraph inside a .card get, and why?
Show answer
It gets 1rem. The first rule has specificity (0,0,1), because :where(.card) adds nothing and only p counts. The second rule has (0,0,1) too. The specificity ties, so the later rule in source order wins.