Sub topic 5
How :is() and :where() differ in specificity
This chapter assumes you know type, class, ID and attribute selectors, combinators, comma-separated selector lists, and the basic idea that the cascade uses specificity to settle conflicts. The chapter before this one showed that :is() and :where() match the same elements. They differ only in the specificity they add to a selector.
Specificity as a three-part score
Specificity is written as three counts, (IDs, classes, types). Classes, attributes and pseudo-classes add to the middle count, and type selectors add to the last. Counts are compared left to right, so one ID beats any number of classes.
The two rules
:is()takes the specificity of its most specific argument.:where()always contributes zero, whatever its arguments are.
The rest of the selector counts as usual.
/* Specificity (1,0,1): the most specific argument, #main, sets the value */
:is(#main, .note) p {
color: blue;
}
/* Specificity (0,0,1): :where() contributes nothing */
:where(#main, .note) p {
color: blue;
}In the first rule, :is() contributes (1,0,0) from #main, and p adds (0,0,1). In the second rule, only p counts.
The highest score always counts
With :is(), the score comes from the most specific argument in the list. It does not depend on which argument actually matched the element. Take :is(#main, .note) p and a paragraph inside <div class="note">. Only .note matched, but the rule still scores (1,0,1).
This surprises many people. A single ID in the list raises the specificity for every element the rule matches.
Why this matters for overrides
Here is a default style written with :where():
:where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}The :where() rule scores (0,0,0). The .menu rule scores (0,1,0), so it wins no matter which comes first in the source. You can override the default with the simplest selector, and you never need !important.
Now swap in :is(ul, ol). The score becomes (0,0,1). A later .menu rule still wins, because a class beats a type. A plain ul { padding-left: 0; } is a different case. It ties with :is(ul, ol) at (0,0,1), so source order decides. With :where(), that plain ul rule wins even when it comes earlier in the file.
Comparing the two
| Selector | Specificity |
|---|---|
| `:is(h1, h2)` | (0,0,1) |
| `:is(.a, h1)` | (0,1,0) |
| `:where(.a, #b)` | (0,0,0) |
| `:where(.a) p` | (0,0,1) |
| `.box :is(#x, p)` | (1,1,0) |
Common mistakes
- Assuming
:is()scores only the matched argument. It always scores the highest one in the list. - Putting an ID in an
:is()list "just in case". It raises the specificity of the whole rule. - Using
:where()for styles that must win. Its zero specificity loses to almost any other rule.
Exercise
What is the specificity of nav :is(.active, #current) a, and what is it if :is is replaced with :where?
Show answer
With :is(), it is (1,0,2). The highest argument, #current, gives (1,0,0), and nav and a add two types. This holds even for elements that matched only .active. With :where(), it is (0,0,2), because only nav and a count.