Sub topic 13
:is(), :where(), and :not() and Specificity
This chapter assumes you can write type, class, ID, and attribute selectors. It also assumes you can read a specificity triple written as (IDs, classes/attributes/pseudo-classes, types/pseudo-elements). You should know how combinators work and how the cascade breaks ties by source order.
Why these three pseudo-classes matter
Most pseudo-classes, such as :hover or :first-child, add one to the class column of specificity. Three functional pseudo-classes behave differently. They take a selector list as an argument, and each one handles specificity in its own way:
:is()matches if any selector in its list matches. It takes the specificity of its most specific argument.:where()matches the same way as:is(). Its specificity is always zero.:not()matches if none of the selectors in its list match. It takes the specificity of its most specific argument.
The functional pseudo-class itself adds nothing. Only the arguments count.
Reducing repetition with :is()
Without :is(), you repeat the shared parts of a selector:
article h2,
article h3,
section h2,
section h3 {
margin-top: 0;
}With :is(), the same rule is shorter:
:is(article, section) :is(h2, h3) {
margin-top: 0;
}Both versions match the same elements. The specificity is the same too, because every argument here is a single type selector. Each :is() contributes (0,0,1), so the whole selector is (0,0,2).
How :is() calculates specificity
The browser looks at every selector in the argument list and uses the highest one. It does this even when the element matched through a weaker argument. Consider this example:
:is(.a, #b) p { color: blue; }The list contains .a (0,1,0) and #b (1,0,0). The highest is #b, so :is(.a, #b) counts as (1,0,0). Adding the p, the full selector is (1,0,1).
This holds for an element inside <div class="a">, which matches only through .a. The selector still carries ID-level specificity. The browser does not check which argument matched.
:where() with zero specificity
:where() has the same syntax and matching behavior as :is(). The only difference is that it always contributes (0,0,0), whatever its arguments contain:
:where(.a, #b) p { color: blue; }Here the specificity is (0,0,1), from the p alone. Any other rule targeting the same p with a class, an ID, or even a bare p selector placed later in the source will override it.
This makes :where() useful for defaults that you expect to be overridden, such as a base stylesheet or a component library:
:where(ul, ol) {
padding-inline-start: 1.5rem;
}
.menu {
padding-inline-start: 0;
}The .menu rule wins without any extra work. Had the first rule used :is(ul, ol), its specificity would be (0,0,1), which .menu at (0,1,0) would still beat. The gap matters more with larger selectors, as the next section shows.
Using :not()
:not() excludes elements. It accepts a selector list, and an element matches only if it matches none of them:
p:not(.hidden) { color: blue; }The specificity is (0,1,1). The p adds (0,0,1), and the argument .hidden adds (0,1,0). The :not() wrapper adds nothing.
A list in :not() uses the most specific argument, as in :is():
a:not(.external, #home) { color: navy; }This has specificity (1,0,1). The #home argument raises the selector to ID level even though most links will match through the class exclusion.
A common pitfall: :not() raises specificity
Because :not(.class) counts as a class, it is easy to write a selector that is stronger than you intended:
.card:not(.compact) .title { font-size: 1.25rem; } /* (0,3,0) */
.card .title { font-size: 1rem; } /* (0,2,0) */
.card.compact .title { font-size: 0.9rem; } /* (0,3,0) */For a .compact card, the first rule does not match, so the third rule wins. For a regular card, the first rule beats the second. The first rule's (0,3,0) is also equal to the third rule's, so source order would decide if both could match the same element.
Comparing the three side by side
| Pseudo-class | Matches when | Specificity contributed |
|---|---|---|
| `:is(list)` | any selector in the list matches | highest argument |
| `:where(list)` | any selector in the list matches | always (0,0,0) |
| `:not(list)` | no selector in the list matches | highest argument |
Worked example
Take this markup and stylesheet:
<nav class="site-nav" id="top">
<a href="/" class="current">Home</a>
<a href="/about">About</a>
</nav>:where(nav) a { color: gray; } /* (0,0,1) */
:is(.site-nav, #top) a { color: black; } /* (1,0,1) */
nav a:not(.current) { color: blue; } /* (0,1,2) */
nav a.current { color: green; } /* (0,1,2) */Work through each link:
- The "Home" link has class
current. Rules 1, 2, and 4 match. Rule 3 does not, because of:not(.current). Rule 2 has the highest specificity at (1,0,1), so the link is black. Rule 4 never gets a chance, because its (0,1,2) loses to (1,0,1). - The "About" link matches rules 1, 2, and 3. Rule 2 wins again at (1,0,1), so it is black.
The result is surprising if you expected blue and green. The :is() rule included an ID in its list, which gave the entire rule ID-level specificity for every link. Removing #top from the list, or switching to :where(), restores the behavior you probably wanted.
:where(.site-nav, #top) a { color: black; } /* (0,0,1) */With this change, "About" is blue (rule 3 at (0,1,2)) and "Home" is green (rule 4 at (0,1,2) beats rule 1 at (0,0,1), and rule 3 does not match).
Edge cases
- Forgiving selector lists. In
:is()and:where(), an invalid selector in the list is ignored and the rest still work. In:not(), current browsers also accept a selector list, but older ones accepted only a single simple selector. Check your browser support targets before relying on lists in:not(). - Pseudo-elements.
:is()and:where()cannot match pseudo-elements such as::before. Writing:is(p::before)matches nothing. - Nesting.
:is()is also what the browser uses internally to resolve the parent selector in native CSS nesting, so a nested rule inside.a, #b { ... }takes the specificity of the highest parent selector. - Empty or unmatched lists.
:is()with no matching elements simply matches nothing and does not break the rest of the stylesheet.
Choosing between them
Use :where() for base styles, resets, and library defaults that other code should be able to override easily. Use :is() to shorten selectors when you want normal specificity. Use :not() when excluding a case is clearer than overriding it, and remember the excluded class counts toward the total.
The layered approach covered in later chapters works together with :where(). Layers control which group of rules wins, and :where() keeps rules inside a group easy to override.
Summary
- The wrapper
:is(),:where(), or:not()never adds specificity by itself. :is()and:not()use the highest-specificity argument in their list.:where()is always (0,0,0).- One strong argument in a list raises the whole selector, even if the element matched through a weaker one.
Exercise
Calculate the specificity of each selector: (a) :is(h1, .title) span, (b) :where(#app, .page) .item, (c) li:not(#first, .last).
Show answer
(a) The highest argument in :is() is .title at (0,1,0), and span adds (0,0,1), so the result is (0,1,1). (b) :where() contributes (0,0,0), and .item adds (0,1,0), so the result is (0,1,0). (c) The highest argument in :not() is #first at (1,0,0), and li adds (0,0,1), so the result is (1,0,1).
Exercise
A stylesheet has :is(.sidebar, #main) a { color: red; } and later .sidebar a.active { color: green; }. A link with class active sits inside .sidebar. Which color does it get, and how could you change the first rule so green wins?
Show answer
The link is red. The first rule has specificity (1,0,1) because #main is in the list, and the second rule has only (0,2,1). To make green win, replace :is() with :where() so the first rule drops to (0,0,1), or remove #main from the list.