Sub topic 5
How :is() and :where() differ in specificity
This chapter assumes you know basic selectors, combinators, comma-separated selector lists, and the idea that the cascade uses specificity to pick a winner between conflicting rules. It also assumes you have seen :is() and :where() match a list of alternatives. The only thing that separates the two is how much they add to specificity.
The one difference
Both pseudo-classes take a selector list and match an element if any item in the list matches it. They differ in one respect:
:is()adds the specificity of its most specific argument.:where()adds zero, whatever its arguments are.
Everything else, including matching, forgiving parsing and the restrictions on arguments, is the same.
Specificity in a sentence
Specificity is a three-part value, written (A, B, C):
- A counts ID selectors.
- B counts class selectors, attribute selectors and pseudo-classes.
- C counts type selectors and pseudo-elements.
Compare the parts left to right. (1,0,0) beats (0,99,0) because the A part is compared first. The universal selector * and combinators add nothing.
The pseudo-class :is() itself adds nothing for being a pseudo-class. Only what is inside its parentheses counts. This is an exception to the normal rule that pseudo-classes add to the B part.
How :is() computes its value
The browser finds the argument with the highest specificity and adds that value to the rest of the selector.
/* 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, the arguments are #main (1,0,0) and .note (0,1,0). The ID wins, so :is() contributes (1,0,0). Add p at (0,0,1) and the total is (1,0,1).
In the second rule, :where() contributes (0,0,0). Only p counts, so the total is (0,0,1).
Both rules match exactly the same paragraphs. Only their strength in the cascade differs.
Worked comparisons
| Selector | Specificity | Why |
|---|---|---|
| `article :is(h1, h2, h3) a` | (0,0,3) | `article`, one type from the list, and `a` |
| `:is(.a, .b, #c)` | (1,0,0) | `#c` is the most specific argument |
| `:is(p, .note)` | (0,1,0) | `.note` outranks `p` |
| `:where(#c, .a, p)` | (0,0,0) | `:where()` always adds zero |
| `.menu :where(a, button)` | (0,1,0) | only `.menu` counts |
| `:is(:where(#c), p)` | (0,0,1) | the inner `:where()` is zero, so `p` is the highest |
The last row shows that the two can be nested. Each function applies its own rule to what is inside it.
The most specific argument counts, even when it does not match
This is the most common surprise. The browser does not check which argument matched the element. It always uses the highest value in the list.
<main>
<p class="note">A note.</p>
</main>:is(#main, .note) p {
color: blue;
}Suppose a different element matches only .note. The rule still carries (1,0,1), because #main is in the list. A rule written this way can beat selectors you did not expect it to beat.
Two practical consequences follow:
- Keep ID selectors and other high-specificity arguments out of an
:is()list unless you want the whole list to be that strong. - If one argument is much stronger than the others, the weaker ones gain strength they would not have on their own.
Using :where() to make rules easy to override
Because :where() adds nothing, a rule built mostly from :where() has very low specificity. Any later or more specific rule overrides it without a fight.
:where(ul, ol) {
margin: 0;
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}The :where() rule has specificity (0,0,0). The .menu rule has (0,1,0). The class rule wins for any element with class="menu", even if the :where() rule comes later in the file. For a plain <ul> with no class, nothing competes, so the defaults apply.
Compare that with the same rule written using :is():
:is(ul, ol) {
padding-left: 1.5rem;
}
.menu {
padding-left: 0;
}Here the first rule has (0,0,1) and .menu has (0,1,0), so .menu still wins. The difference shows when the argument is stronger. With :is(ul, ol, .list), the first rule becomes (0,1,0), and then source order decides against .menu. With :where(ul, ol, .list), the rule stays at (0,0,0) and .menu wins regardless of order.
A :where() rule can also be overridden by the plainest type selector. A bare ul { padding-left: 0; } has (0,0,1), which beats (0,0,0).
Mixing :where() into a larger selector
Only the :where() part is free. The rest of the selector still counts.
/* (0,1,1): .prose and a count, :where() adds nothing */
.prose :where(h2, h3) a {
text-decoration: underline;
}
/* (0,1,2): :is() would add the type selector for h2 or h3 */
.prose :is(h2, h3) a {
text-decoration: underline;
}You can use this to keep the structure of a selector while controlling how strong it is. Put the parts you want to stay overridable inside :where(), and leave the parts that should carry weight outside it.
A step-by-step method for predicting specificity
- Replace each
:where(...)with nothing. It contributes (0,0,0). - For each
:is(...), compute the specificity of every argument. A complex argument such asul licounts as a whole, so it is (0,0,2). - Take the highest value among the arguments of that
:is(). - Add that value to the counts for the remaining parts of the selector.
- Compare the final value with the competing rules, A first, then B, then C.
When two arguments are both complex, compare them as full triples. :is(.a .b, #c) has the arguments (0,2,0) and (1,0,0). The ID wins on A, so the value is (1,0,0), even though .a .b has more classes.
Common mistakes
- Expecting
:is()to behave like:where(). A rule that uses:is()with an ID is hard to override later. - Using
:where()for rules that must win. They lose to nearly everything, including a bare type selector. - Assuming the matching argument sets the value. The strongest argument sets it, matched or not.
- Counting
:is()as one pseudo-class (0,1,0). It does not add a class-level value on its own. - Forgetting that
!importantand cascade layers sit above specificity. Neither function changes how those work.
Exercises
Exercise
What is the specificity of nav :is(.menu, #site-nav) li, and what is it if :is is replaced with :where?
Show answer
With :is(), the most specific argument is #site-nav at (1,0,0). Add nav (0,0,1) and li (0,0,1) for a total of (1,0,2). With :where(), the function adds nothing, so the total is (0,0,2).
Exercise
A stylesheet has :where(a, button) { color: gray; } followed by a { color: green; }. A link on the page matches both rules. What color is it, and why? What if the first rule used :is() instead?
Show answer
The link is green. The :where() rule has specificity (0,0,0) and the a rule has (0,0,1), so a wins. With :is(a, button), the first rule would be (0,0,1), the same as a. The tie is then broken by source order, and the later a rule still wins, so the link is green. If the order were swapped, the :is() rule would win and the link would be gray, but the :where() rule would still lose.
Exercise
The rule :is(#hero, .card) p { margin: 0; } is applied to a paragraph inside .card, where there is no #hero anywhere. What is the specificity of the rule for that paragraph?
Show answer
(1,0,1). The browser uses the most specific argument in the list, #hero, even though only .card matched.