Sub topic 4
Matching alternatives with :where()
This chapter assumes you know type, class, ID and attribute selectors, the descendant and child combinators, comma-separated selector lists, and the basic idea that specificity decides which conflicting rule wins. It also assumes you have met :is() in the previous chapter.
What :where() does
:where() is a functional pseudo-class. It takes a list of selectors, separated by commas, and matches any element that matches at least one of them. Compare it with :is(). Both match the same elements for the same argument list. The one difference is that a selector inside :where() adds nothing to specificity.
Here is the smallest useful example:
:where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
}This rule matches every ul and every ol. Without :where() you would write ul, ol { ... }, which matches the same elements. The two rules differ in specificity, which the next section covers.
Syntax and placement
The argument is a selector list. Each item can be a compound selector such as a.external, or a complex selector that contains combinators such as nav > ul.
:where(.sidebar > ul, .footer ul) li {
list-style: none;
}You can put :where() anywhere a compound selector could go: at the start, in the middle, at the end, or attached to another selector.
| Pattern | Meaning |
|---|---|
| `:where(h1, h2, h3)` | Any `h1`, `h2` or `h3` |
| `article :where(h1, h2, h3) a` | A link inside a heading inside an `article` |
| `a:where(.external, [target="_blank"])` | An `a` that has the class `external` or a `target="_blank"` attribute |
| `:where(.card, .panel) :where(h2, h3)` | Two lists combined with the descendant combinator |
You can nest :where() inside :where() or inside :is(), and the other way round. Nesting is rarely needed. Reach for it only when it makes the rule easier to read.
An empty list, :where(), is valid. It matches nothing.
The zero-specificity rule
Specificity is written as three numbers: IDs, then classes (including attribute selectors and pseudo-classes), then types and pseudo-elements. The rule for :where() is simple: the function and everything inside it count as (0,0,0).
Only the parts of the selector outside the function count. The table shows the effect.
| Selector | Specificity |
|---|---|
| `p` | (0,0,1) |
| `.menu` | (0,1,0) |
| `:where(#main, .note) p` | (0,0,1) |
| `:is(#main, .note) p` | (1,0,1) |
| `:where(.card) .title` | (0,1,0) |
| `:where(ul, ol)` | (0,0,0) |
The fourth row is the contrast with :is(), whose specificity comes from its most specific argument. A later chapter compares the two in depth. For now, remember that :where() hides its contents from the specificity calculation.
The last row is unusual. A rule with specificity (0,0,0) loses to any other rule that matches the element and sets the same property, including a plain p selector. It also loses to the browser's default stylesheet only if that stylesheet has higher origin priority, but in practice author rules always beat the browser defaults, so a (0,0,0) rule still replaces them.
Worked example: defaults that are easy to override
The main use of :where() is a default style that later rules can replace without a specificity fight. Suppose a shared stylesheet sets list spacing:
:where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}Take a ul class="menu". Both rules match it and both set padding-left. The .menu rule has specificity (0,1,0) and the :where() rule has (0,0,0), so .menu wins. This holds even if the :where() rule appears later in the file, because source order only breaks ties between equal specificities.
Now try the same thing with a plain list selector:
ul, ol {
margin: 0;
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}Here .menu also wins, because .menu (0,1,0) beats ul (0,0,1). The difference appears when the default is more specific. Consider this default:
main ul {
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}main ul has specificity (0,0,2), and .menu still wins. A default only becomes a problem when it grows to include classes or IDs, for example main .content ul. That selector is (0,1,2) and beats .menu. Wrap the context in :where() and the default stays weak:
:where(main .content) ul {
padding-left: 1.5rem;
}This rule has specificity (0,0,1), which comes only from the trailing ul. The context still has to match, so the rule only applies to lists inside main .content, but it adds no weight.
A complete page you can run
Save this as an HTML file and open it in a browser to see the effect.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>:where() demo</title>
<style>
:where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
color: gray;
}
.menu {
padding-left: 0;
list-style: none;
}
#special {
color: green;
}
</style>
</head>
<body>
<ul>
<li>Default list: gray, indented</li>
</ul>
<ul class="menu">
<li>Menu: gray, no indent, no bullets</li>
</ul>
<ol id="special">
<li>Special: green, because the ID rule beats the zero-specificity rule</li>
</ol>
</body>
</html>The ordered list is green even though :where(ul, ol) sets color: gray. The default rule was written first and lost on specificity, not on order.
How a rule with :where() competes
The diagram shows the decision for one element and one property when two rules match.
Common errors
Expecting :where() to raise specificity. Authors sometimes wrap a selector in :where() and then wonder why the rule loses. It loses because it is meant to. Use :where() for defaults and :is() or a plain selector for rules that must win.
Assuming the whole selector is zero. Only the content of the function is ignored. In :where(.card) .title, the .title class still counts, so the specificity is (0,1,0), not (0,0,0).
Putting a pseudo-element inside the list. Pseudo-elements such as ::first-line and ::before are not allowed in the argument list, so a rule written that way never matches. Put the pseudo-element after the function:
/* Works */
:where(p, li)::first-line {
font-weight: bold;
}
/* Never matches */
:where(p::first-line, li::first-line) {
font-weight: bold;
}Forgetting that !important ignores this. Specificity trick or not, an !important declaration in one rule beats a normal declaration in another. :where() only controls the weight of the selector.
Using it where a reset would be wrong. A zero-specificity default is easy to override by accident. If a component must keep a style no matter what else is on the page, do not put that style behind :where().
Checking your understanding
Exercise
A stylesheet contains :where(.card, .panel) h2 { color: navy; } and, earlier in the file, h2 { color: black; }. An h2 sits inside .card. What colour is it, and why?
Show answer
Navy. The :where() rule has specificity (0,0,1), from the h2 outside the function. The plain h2 rule also has (0,0,1). The specificities are equal, so source order decides, and the :where() rule comes later. If the :where() rule came first, black would win.
Exercise
You want a default a { text-decoration: underline; } that applies only inside nav or footer, and that any single class selector can override. Write the rule and give its specificity.
Show answer
:where(nav, footer) a { text-decoration: underline; }. The specificity is (0,0,1), from the a outside the function. A rule such as .plain { text-decoration: none; } has (0,1,0) and overrides it.