Sub topic 5
Specificity of :is(), :where() and :not()
This chapter assumes you can already count specificity for plain selectors as an (ids, classes, elements) triple, such as #nav .item a giving (1,1,1). You should also know that when two rules tie on specificity, the later one in source order wins. If either idea is shaky, review the specificity and source order chapters first.
Three pseudo-classes take a selector list as an argument and change how specificity is counted: :is(), :where() and :not(). Each handles its argument differently, and the differences matter when you write reset styles, component libraries and overrides.
The three rules in one table
| Selector | Specificity contributed | Rule |
|---|---|---|
| `:is(A, B)` | That of the most specific argument | Highest argument wins, whether or not it matches |
| `:not(A, B)` | That of the most specific argument | Same rule as `:is()`. The negation itself adds nothing |
| `:where(A, B)` | (0,0,0) | Always zero, whatever the arguments are |
The pseudo-class itself adds nothing. A normal pseudo-class such as :hover counts as a class, so it adds (0,1,0). :is(), :not() and :where() are exceptions, because only their arguments count.
:is() takes the most specific argument
:is() matches an element if it matches any selector in its list. For specificity, the browser does not look at which argument matched. It uses the highest-scoring argument in the list.
:is(#intro, .note, p) { color: green; }
.note { color: blue; }<p class="note">Hello</p>The element is a p with class note, and it has no id. Even so, :is(#intro, .note, p) scores (1,0,0), because #intro is the most specific argument. The first rule beats .note (0,1,0), so the paragraph is green.
This is a common source of confusion. A long list inside :is() makes the whole selector as heavy as its heaviest member, and that weight applies even when the element only matches a lighter member.
Some concrete scores:
:is(.a, .b) pis (0,1,1). Both arguments score (0,1,0), andpadds (0,0,1).:is(#a, .b) pis (1,0,1).:is(.card p, .list li)is (0,1,1). A compound argument is scored in full, so.card pis (0,1,1) and.list liis also (0,1,1).:is(.a, .b.c)is (0,2,0), because.b.cis the highest argument.
Why :is() is useful anyway
:is() shortens repetitive selectors. These two blocks are equivalent in what they match:
/* Without :is() */
article h1, article h2, article h3 { margin-block: 1em; }
/* With :is() */
article :is(h1, h2, h3) { margin-block: 1em; }Their specificity is also the same, (0,0,2), because every heading is a single element selector. The shorter form is easier to maintain.
:is() also uses a forgiving selector list. If one argument is invalid or unsupported, the browser ignores that argument and keeps the rest. Without :is(), a single invalid selector in a comma-separated list makes the whole rule invalid. Forgiveness does not change the scoring rule. Only the valid arguments are scored.
:where() always contributes zero
:where() accepts the same arguments as :is() and matches the same elements. Its specificity is always (0,0,0), however specific the arguments are.
:where(.prose) a { color: blue; }
a { color: red; }The first selector is :where(.prose) a. Only the trailing a counts, so the score is (0,0,1). The second selector a also scores (0,0,1). The specificity ties, so source order decides, and the later rule makes links red.
Here is the same pair with :is():
:is(.prose) a { color: blue; }
a { color: red; }Now the first selector scores (0,1,1) and wins no matter how the rules are ordered. Links inside .prose become blue.
Low-priority defaults
Because :where() adds no weight, it suits default styles that any later rule can override with a simple selector:
:where(.btn) {
padding: 0.5rem 1rem;
border-radius: 4px;
background: #eee;
}
.btn-primary { background: steelblue; }:where(.btn) scores (0,0,0). .btn-primary scores (0,1,0), so it overrides the background even if the library's stylesheet is loaded after yours. A consumer of the library does not need a heavier selector or !important.
Two limits apply:
:where()only lowers the weight of the selector it appears in. It does not affect other rules.- A rule with zero specificity still beats inherited values. Inheritance applies only when no rule targets the element at all.
:not() counts its arguments, not the negation
:not() matches elements that do not match any selector in its list. Like :is(), it takes the specificity of its most specific argument.
p:not(.note) { color: gray; }
p.note { color: blue; }p:not(.note) is (0,1,1), from the element p plus the class argument. p.note is also (0,1,1). That is a tie, so the later rule wins for any element matched by both. In this example no element can match both, because one needs .note and the other forbids it.
The argument's weight applies even though the selector excludes elements:
a:not(#footer-link) { color: black; }
a.external { color: purple; }For an anchor that is not #footer-link, the first rule scores (1,0,1) because the argument is an id. The second rule scores (0,1,1). The first rule wins, so external links stay black, which is probably not what the author wanted.
With a list, the highest argument sets the weight:
:not(.a, .b)is (0,1,0).:not(.a, #b)is (1,0,0).:not(.a):not(#b)is (1,1,0), because two separate:not()pseudo-classes each add their own score.
The last two examples look alike but score differently. A list inside one :not() takes the maximum. Chained :not() selectors add together. Chaining raises specificity, sometimes unintentionally.
Related pseudo-classes
Two other selectors follow the same pattern and are worth knowing:
:has()takes the specificity of its most specific argument, like:is().a:has(img)is (0,0,2) anda:has(.icon)is (0,1,1).:nth-child(2n+1 of .item)adds (0,1,0) for the pseudo-class, plus the most specific selector in theoflist. So it scores (0,2,0).
The & nesting selector in native CSS nesting is also scored like :is(). It uses the most specific selector of the parent rule. This surprises authors who expect nesting to copy the parent text:
.card, #featured {
& p { color: green; }
}The & p selector behaves as :is(.card, #featured) p, which scores (1,0,1). It does so even for a plain .card element. This is the same "highest argument" rule you saw in :is().
Worked comparison
Consider this markup and stylesheet:
<nav class="menu">
<a href="/home" class="link">Home</a>
</nav>:where(nav) a { color: black; } /* (0,0,1) */
.menu a { color: blue; } /* (0,1,1) */
:is(.menu, #top) a { color: green; } /* (1,0,1) */
a:not(.menu) { color: orange; } /* (0,1,1) */
nav a.link:where(:hover) { color: red; } /* (0,1,2) */The link matches all five rules. Their scores in order:
:where(nav) ais (0,0,1).:where()adds nothing, so onlyacounts..menu ais (0,1,1).:is(.menu, #top) ais (1,0,1). The#topargument sets the weight, even though the nav has no such id.a:not(.menu)is (0,1,1). The anchor does not have classmenu, so this rule matches.nav a.link:where(:hover)is (0,1,2), fromnav,a,.link. The hover state is hidden inside:where(). It applies only while the pointer is over the link.
When the pointer is not over the link, the third rule wins with (1,0,1), and the link is green. When the pointer is over the link, rule 5 still scores (0,1,2), which loses to (1,0,1). The link stays green even on hover. To make hover work, you would need to raise the weight of the hover rule, or lower the weight of the green rule by using :where(.menu, #top) a.
Common mistakes
- Assuming that
:is()scores by whichever argument matched. It scores by the highest argument. - Forgetting that
:not(#id)adds an id's weight even though it excludes that element. - Using
:is()for defaults that others need to override. Use:where()instead. - Wrapping a state pseudo-class such as
:hoverin:where()and then wondering why the hover rule loses to a stronger base rule. - Expecting
:where()to beat inherited styles from a parent. It does not need to, since a matching rule always beats inheritance. - Adding a high-specificity argument to a long
:is()list for one case. Every other case in the list now carries that weight.
Debugging scores in developer tools
Most browsers show which rules matched in the Styles panel, ordered by precedence, and strike through declarations that lost. Hovering over a selector in Chrome and Firefox shows its specificity, and in Firefox the value is displayed as a triple. When a rule loses unexpectedly, check each :is(), :not() and :has() argument list for a heavy member such as an id.
Summary
:is(),:not()and:has()take the specificity of their most specific argument.:where()always contributes (0,0,0).- An argument's weight counts whether or not it is the one that matched.
- Chained
:not()selectors add together, while a list inside one:not()takes the maximum. - Use
:where()for overridable defaults and:is()for brevity.
Exercise
What is the specificity of ul :is(.menu, #nav) > li:where(.active), and does it beat the selector #nav li.active?
Show answer
It scores (1,0,2): ul and li add two elements, :is(.menu, #nav) adds an id from its highest argument #nav, and :where(.active) adds nothing. The selector #nav li.active scores (1,1,1), so it wins, because a class beats an extra element at equal id counts.
Exercise
A library ships :is(.btn) { background: #eee; } and you cannot edit it. You write .btn-primary { background: steelblue; } earlier in the source order than the library. Which background does a button with both classes get, and what change to the library rule would fix it without touching your CSS?
Show answer
The button gets #eee, because :is(.btn) scores (0,1,0), which ties with .btn-primary at (0,1,0), and the library rule comes later and wins. Changing the library rule to :where(.btn) makes it (0,0,0), so .btn-primary wins regardless of order.