Sub topic 7
Writing defaults that are easy to override
This chapter assumes you know basic selectors, combinators, comma-separated selector lists, and how specificity decides which rule wins in the cascade. It also assumes you have met :is() and :where() and know the key difference: :is() takes the specificity of its most specific argument, and :where() always contributes zero.
Why defaults cause trouble
A default style is a rule that sets a sensible baseline, such as list spacing, link underlines, or form control sizes. Later, a component or a page needs to change it. If the default has high specificity, every override has to match or beat that specificity. This leads to longer selectors, extra classes, or !important.
Consider a reset written with ordinary selectors:
article ul,
article ol {
margin: 0;
padding-left: 1.5rem;
}Each selector here has specificity (0,0,2): two type selectors. A single class such as .menu has specificity (0,1,0), so it wins. But a reset that mentions classes or IDs is harder to override:
.prose ul,
.prose ol {
margin: 0;
padding-left: 1.5rem;
}Now the default has specificity (0,1,1). A plain .menu rule loses, and the author has to write .prose .menu or something stronger to win. That coupling to the default's structure is what you want to avoid.
Zero specificity with :where()
Wrapping the whole selector in :where() removes its specificity. The rule still matches the same elements, but it loses every tie-break.
:where(.prose) :where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}The default rule has specificity (0,0,0). The .menu rule has (0,1,0), so it overrides the default no matter where it appears in the source. You can place the default after the component rules and it still loses.
Specificity compared:
| Selector | Specificity |
|---|---|
| `.prose ul` | (0,1,1) |
| `:is(.prose) ul` | (0,1,1) |
| `:where(.prose) ul` | (0,0,1) |
| `:where(.prose) :where(ul)` | (0,0,0) |
Only the arguments of :where() are neutralized. Anything you write outside it still counts. So to get a fully zero-specificity rule, every part of the selector, including type selectors, must be inside :where().
Source order still breaks ties
When two rules both have specificity (0,0,0), the later one wins. This is useful. Two default rules can override each other by position, with no specificity arithmetic.
:where(a) {
color: royalblue;
text-decoration: underline;
}
:where(nav a) {
color: inherit;
text-decoration: none;
}Both rules have specificity (0,0,0) or less. The nav a rule appears later, so it wins for links inside nav. Note that :where(nav a) has no specificity at all, even though nav a normally has (0,0,2). Putting more specific defaults after general ones, in source order, gives you a predictable layering inside your defaults.
A worked example: a small design baseline
Suppose you ship a base stylesheet that many pages share. You want sensible form and heading styles, and you want any page-level rule to override them cleanly.
/* base.css: all defaults at zero specificity */
:where(h1, h2, h3, h4) {
margin-block: 1.5em 0.5em;
line-height: 1.2;
}
:where(button, input, select, textarea) {
font: inherit;
padding: 0.5rem 0.75rem;
border: 1px solid #888;
border-radius: 4px;
}
:where(button, [type="submit"]) {
background: #eee;
cursor: pointer;
}/* page.css: simple, single-class overrides */
.cta {
background: #0a6;
color: white;
border-color: transparent;
}A <button class="cta"> gets the base padding and font, but .cta overrides the background and border. The page author did not need button.cta, form .cta, or !important. A single class is enough, and that is the point of the technique.
Compare this with writing the base selector as :is(button, [type="submit"]). The attribute selector inside :is() raises the specificity to (0,1,0). The .cta rule would then tie with it, and the result would depend on source order. If base.css loaded after page.css, the base background would win. Using :where() removes that dependency.
Pick what goes inside :where()
You do not always want everything at zero. A useful pattern is to keep the part that expresses intent as a normal selector and neutralize only the scoping.
/* Scoped default: .prose is a scope, not a styling hook */
:where(.prose) h2 {
font-size: 1.5rem;
}This has specificity (0,0,1). It still beats a global :where(h2) default (0,0,0), but it loses to any class-based rule. That gives you three tiers:
- Global defaults at (0,0,0).
- Scoped defaults at (0,0,1) or similar, which win over global ones.
- Component and page rules with classes, which win over both.
In the diagram, an arrow points from a rule to the rule that overrides it.
Common mistakes
- Leaving a part outside
:where().:where(.prose) ulis (0,0,1), not zero. That is fine when intended. It is a surprise when you expected a pure zero. - Using
:is()for defaults.:is(.a, #b) ptakes the specificity of the ID, even when the element matched through.a. Specificity does not depend on which argument matched. - Expecting
:where()to beat higher-specificity rules. It only removes its own contribution. A rule with a class or ID elsewhere still wins. - Pseudo-elements inside the list.
:where(p::first-line)never matches. Put pseudo-elements outside::where(p)::first-line. A pseudo-element outside the function also adds its own (0,0,1). - Putting
:where()on overrides. The goal is for defaults to be weak. Overrides should have normal specificity. If you wrap every rule in:where(), you lose the benefit and rely entirely on source order.
Defaults and `!important`
Zero specificity does not change how !important works. An !important declaration in a default beats any normal declaration, however weak its selector. Avoid !important in defaults you want others to override.
Checking your work
To confirm a default is overridable, write a test override with one class and see whether it wins when placed before and after the default in the source. If the result changes with order, the default's specificity is not zero or your override ties with it.
:where(.prose) :where(blockquote) {
border-left: 4px solid #ccc;
padding-left: 1rem;
}
.quote {
border-left-color: tomato;
}Here .quote overrides only the border color. The other declarations from the default still apply, because the cascade resolves each property separately.
Exercise
A stylesheet contains :is(.form, #signup) input { border: 1px solid gray; }. A page adds .field { border: 2px solid blue; } to an input inside .form, and the border stays gray. Explain why, and rewrite the first rule so .field wins regardless of source order.
Show answer
:is() takes the specificity of its most specific argument, #signup, so the rule has specificity (1,0,1) even though the element matched through .form. The .field rule has (0,1,0) and loses. Rewrite it with :where() around everything: :where(:where(.form, #signup) input), or more simply :where(.form, #signup) :where(input), giving specificity (0,0,0). Then .field wins in either source order.
Exercise
What is the specificity of :where(nav) a:hover, and what would you change to make the whole selector (0,0,0)?
Show answer
a adds (0,0,1) and :hover adds (0,1,0), so the total is (0,1,1). :where(nav) adds nothing. To get zero, move everything inside: :where(nav a:hover).