Sub topic 5
Specificity of :is(), :where() and :not()
This chapter assumes you can already calculate specificity as an (ids, classes, elements) triple and that you know the cascade compares specificity before source order. Here you will learn how three selector functions change that count.
Why these functions need their own rules
Most selectors have a specificity you get by counting their parts. The functional pseudo-classes :is(), :where() and :not() take a list of selectors as an argument. The browser has to decide how much that list contributes. The three functions follow different rules, and the differences affect how you structure your CSS.
The rules
| Selector | Contribution to specificity |
|---|---|
| `:is(A, B, C)` | The specificity of the most specific selector in the list |
| `:not(A, B, C)` | The specificity of the most specific selector in the list |
| `:where(A, B, C)` | Always (0,0,0) |
The pseudo-class itself adds nothing. Only its arguments count. The browser takes the highest-scoring argument whether or not that argument is the one that matched the element.
/* (1,0,1): the #page argument scores (1,0,0), plus p scores (0,0,1) */
:is(#page, .wide) p { color: green; }
/* (0,1,1): .wide scores (0,1,0), plus p scores (0,0,1) */
:is(.wide, section) p { color: purple; }The first rule scores (1,0,1) even on a page where the paragraph sits inside .wide and has no #page ancestor. The id in the list sets the score, not the match.
:is() hides a high-specificity argument
Because :is() uses its most specific argument, one id in the list raises the score of every alternative. A short selector can then win in surprising places.
:is(.card, #featured) h2 { color: navy; } /* (1,0,1) */
.card .title h2 { color: crimson; } /* (0,2,1) */An h2 inside .card gets navy, even though the second rule looks more detailed. The id in the first list makes it (1,0,1), which beats (0,2,1). Keep high-specificity selectors out of :is() lists unless you want that effect.
:where() for low-priority defaults
:where() works like :is() but counts as zero. Use it for defaults that later rules can override without a specificity fight.
:where(.prose) a { color: blue; }
a { color: red; }
/* Both selectors score (0,0,1), so the later rule wins: links are red. */Compare this with .prose a, which scores (0,1,1) and would beat the plain a rule. Wrapping .prose in :where() removes the class from the count. This pattern suits resets, base styles and library defaults, because anyone using them can override the styles with a simple selector.
:not() counts its argument, not itself
:not(.hidden) scores (0,1,0), the same as .hidden. The negation does not subtract anything. Like :is(), it uses the most specific argument in the list.
a:not(.external) { color: teal; } /* (0,1,1) */
a:not(.external, #top) { color: gray; } /* (1,0,1) */The second rule scores (1,0,1) because #top is in the list. This happens even though most links on the page are not #top.
Common mistakes
- Assuming
:is()scores only the argument that matched. It always uses the highest score in the list. - Assuming
:not()lowers specificity. It adds the score of its argument. - Using
:where()for styles that must win. Its zero score loses to almost any other selector, so reserve it for defaults. - Overlooking that only the arguments count.
a:is(a)scores (0,0,2), not (0,0,1)+(0,1,0).
Choosing between them
Use :where() when you want a rule to be easy to override. Use :is() to shorten a long list of selectors when you accept the highest score. Use :not() to exclude cases. In all three, a #id argument costs much more than it looks.
Exercise
What is the specificity of nav :is(.menu, #main) a:not(.active), and which of these two rules wins for a link inside <nav id="main"> that has no active class: that rule, or nav .menu li a { color: red; }?
Show answer
Count the parts: nav is (0,0,1). :is(.menu, #main) takes its highest argument, #main, which is (1,0,0). a is (0,0,1). :not(.active) is (0,1,0). The total is (1,1,2). The other rule scores (0,2,3): two classes... actually one class .menu is (0,1,0) plus elements nav, li and a (0,0,3), so it scores (0,1,3). The first rule's (1,1,2) beats (0,1,3), because the id column is compared first. The :is() rule wins.
Exercise
A stylesheet has :where(.btn) { background: gray; } followed by button { background: steelblue; }. A <button class="btn"> appears on the page. What colour is its background, and why?
Show answer
Steelblue. The :where(.btn) rule scores (0,0,0) and button scores (0,0,1), so the second rule wins on specificity. Source order does not matter here.