Sub topic 13
:is(), :where(), and :not() and specificity
This chapter assumes you can calculate specificity as an (IDs, classes/attributes/pseudo-classes, types/pseudo-elements) triple. It also assumes you know that equal specificity is settled by source order.
Why these three pseudo-classes matter
:is(), :where(), and :not() are functional pseudo-classes. Each takes a selector list as its argument. They shorten repetitive selectors, but each one affects specificity in its own way. That effect is where most mistakes happen.
The specificity rules
| Selector | Specificity contributed |
|---|---|
| `:is(A, B)` | The most specific argument in the list |
| `:not(A, B)` | The most specific argument in the list |
| `:where(A, B)` | Always (0,0,0) |
The pseudo-class itself adds nothing. Only its arguments count.
:is(.a, #b) p { color: blue; } /* (1,0,1) */
:where(.a, #b) p { color: blue; } /* (0,0,1) */
p:not(.hidden) { color: blue; } /* (0,1,1) */In the first rule, #b is the most specific argument, so the whole :is() counts as (1,0,0). Add the p and the total is (1,0,1). This holds even when the element matched only through .a. The browser does not check which argument matched. It always uses the highest one.
In the third rule, :not(.hidden) counts as .hidden, which is (0,1,0). Add p and the total is (0,1,1). The negation is ignored. Only the argument matters.
Grouping with :is()
Without :is(), you repeat the shared part of a selector:
header a:hover,
main a:hover,
footer a:hover {
text-decoration: underline;
}With :is(), you write it once:
:is(header, main, footer) a:hover {
text-decoration: underline;
}Both versions give the same specificity, (0,1,2). That is one pseudo-class (:hover) and two types (a plus the largest type argument).
Zero specificity with :where()
:where() matches the same elements as :is(), but its specificity is zero. That makes it good for defaults that any later rule can override:
:where(article, section) a {
color: navy;
}
.card a {
color: crimson;
}The first rule has specificity (0,0,1). The second has (0,1,1), so links inside .card are crimson no matter which rule comes first in the file. If you had written :is(article, section) a, that rule would be (0,0,2). The second rule would still win, but only because of its class. Source order would matter for any rule with equal specificity.
Excluding elements with :not()
:not() matches elements that do not match its argument. Because it takes the argument's specificity, a negation can be heavier than it looks:
li:not(#current) { opacity: 0.6; } /* (1,0,1) */
li.item { opacity: 1; } /* (0,1,1) */The first rule matches any li that is not #current. Its specificity is (1,0,1), so it beats li.item and the items stay faded. If you want a negation that stays light, use a class argument such as :not(.current).
Common errors
- Assuming
:is()uses the specificity of the argument that matched. It uses the highest one in the list. - Putting an ID inside
:is()or:not()and then wondering why later class rules lose. - Using
:where()for rules that you need to win. Its zero specificity makes it lose to almost everything. - Putting pseudo-elements such as
::beforeinside:is()or:where(). They are not valid there, so the rule will not match.
Exercise
What is the specificity of :not(.draft, #preview) h2, and does it beat main .post h2 (0,1,2)?
Show answer
:not() takes its most specific argument, which is #preview, so it counts as (1,0,0). Adding h2 gives (1,0,1). That beats (0,1,2), because the ID column is compared first and 1 is greater than 0.