Sub topic 4
Matching alternatives with :where()
This chapter assumes you know type, class, ID and attribute selectors, combinators, comma-separated selector lists, and the basic idea that specificity decides which rule wins in the cascade. It also assumes you have read the chapter on :is().
What :where() does
:where() is a functional pseudo-class. It takes a selector list as its argument and matches any element that matches at least one selector in that list. Matching works exactly as it does for :is(). The only difference is specificity: :where() always contributes zero.
You can use it to remove repetition from a selector list:
/* Before */
article h1 a,
article h2 a,
article h3 a {
text-decoration: underline;
}
/* After */
article :where(h1, h2, h3) a {
text-decoration: underline;
}Both versions match the same links. The second version is shorter and easier to edit, because you add a new heading level in one place.
Why zero specificity matters
Specificity is written as three numbers: (IDs, classes, types). Normally, every selector you add makes a rule harder to override. Anything inside :where() is ignored when the browser calculates specificity, so the function adds nothing.
:where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}The :where(ul, ol) rule has specificity (0,0,0). The .menu rule has (0,1,0). So .menu wins, even if you move it above the :where() rule in the file. When two rules have equal specificity, source order decides. Here the specificity is already different, so order does not matter.
Compare this with a plain ul, ol rule. That has specificity (0,0,1), which beats a bare * rule and any other zero-specificity rule. Using :where() makes the default as weak as a rule can be.
Only the contents of the function are ignored. Selectors outside it still count:
/* Specificity (0,1,1): .card counts, :where(h2, h3) does not */
.card :where(h2, h3) {
margin-block: 0;
}Here .card adds a class and nothing in the function counts. The result is (0,1,0). Be careful when you read these values, since a type selector outside the function would add to the total.
How it compares with :is()
| Feature | `:is()` | `:where()` |
|---|---|---|
| Matches any selector in its list | Yes | Yes |
| Specificity | Most specific argument | Always zero |
| Typical use | Shortening selectors | Weak defaults |
The next chapter works through the specificity rules in detail. For now, remember that :where() is the version you choose when you want a rule to be easy to override.
Common errors
- Expecting
:where()to raise specificity. Putting an ID inside it, as in:where(#main) p, gives specificity (0,0,1), not (1,0,1). Rules written this way lose to almost everything else. - Putting pseudo-elements in the list. Pseudo-elements such as
::first-lineare not allowed as arguments, so a selector like:where(p::first-line)never matches. Place them outside, as in:where(p, li)::first-line. - Using it for the rule that must win. If a style has to override others,
:where()works against you. Use:is()or a plain selector. - Forgetting the weak rule can lose to resets. Any rule with at least one type selector outranks a
:where()rule. This is intended, but it can surprise you when a default seems to be ignored.
Exercise
A stylesheet has :where(#sidebar, .panel) a { color: green; } and later a { color: orange; }. Which colour do links inside #sidebar get, and why?
Show answer
Orange. The :where() rule has specificity (0,0,1) from the trailing a, because the ID inside :where() is ignored. The plain a rule has the same specificity (0,0,1), so source order decides, and the later orange rule wins.
Exercise
Rewrite main p, aside p, footer p { line-height: 1.5; } with :where(), and state its specificity.
Show answer
:where(main, aside, footer) p { line-height: 1.5; }. Its specificity is (0,0,1), from the p outside the function.