Sub topic 12
Specificity of :not() and :has()
This chapter covers how :not() and :has() count toward specificity. It assumes you have read the chapters on :is() and :where(), so you know that :is() takes the specificity of its most specific argument and :where() contributes nothing. You should also be comfortable writing specificity as a triple (IDs, classes, types), such as (1,0,2).
Two more pseudo-classes that take selector lists
:not() and :has() are functional pseudo-classes, like :is() and :where(). Each takes a selector list as its argument. They differ from :is() and :where() in what they do:
:not(list)matches an element that does not match any selector in the list.:has(list)matches an element if at least one relative selector in the list matches something related to it. The relation is usually a descendant, and a leading combinator can change it, as in:has(> img)or:has(+ p).
Their specificity rules are the ones you already know from :is(). This chapter explains how those rules apply and where they cause surprises.
The rule
For both pseudo-classes, the pseudo-class itself adds nothing. The specificity of the argument list is added to the compound selector it appears in. The argument list counts as its most specific item.
:not(.a, #b)has specificity(1,0,0), because#bis the most specific argument.:has(> img, .photo)has specificity(0,1,0), because.photooutranksimg. The>combinator adds nothing.
The two special cases from earlier chapters still apply. A :where() inside either pseudo-class contributes zero, and an :is() inside contributes its own most specific argument.
| Selector | Specificity | Reason |
|---|---|---|
| `a:not(.external)` | (0,1,1) | `a` plus `.external` |
| `:not(.a, #b)` | (1,0,0) | `#b` is the most specific argument |
| `:not(.a):not(.b)` | (0,2,0) | Two separate `:not()` calls each add their argument |
| `li:has(> a.active)` | (0,1,2) | `li` plus `a.active` (one class, one type) |
| `.card:has(img, #hero)` | (1,1,0) | `.card` plus `#hero` |
| `.card:not(:where(.x))` | (0,1,0) | `:where()` adds nothing |
The argument counts even when it does not decide the match
Specificity is a property of the selector text, not of the element that matched. Consider this rule:
.btn:not(.a, #b) {
color: navy;
}Every .btn that lacks both .a and #b matches. The rule has specificity (1,1,0) for all of them, even though no element ever needs #b to match. A single ID in the list raises the specificity of the whole rule. This is the same behavior as :is(#main, .note) in the earlier chapter.
Gotcha: one `:not()` with a list versus chained `:not()`
These two selectors match the same elements:
/* One :not() with a list: specificity (0,1,0) */
:not(.a, .b) {
margin: 0;
}
/* Two chained :not(): specificity (0,2,0) */
:not(.a):not(.b) {
margin: 0;
}Chaining adds each argument separately. A list adds only the largest one. The list form is the right choice when you want to exclude several things without winning more conflicts. Chaining is useful only when you want the extra weight.
Selector lists inside :not() work in all current browsers. Older code often chained :not() because the Selectors Level 3 version allowed only one simple selector.
Gotcha: a `:not()` can make a rule hard to override
Because :not(.disabled) adds a class worth of specificity, a rule that looks simple can beat a later, more obvious one. Here is a complete page:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>:not() specificity</title>
<style>
.btn { background: gray; color: white; padding: 0.5rem 1rem; }
/* (0,3,0): .btn + .disabled + :hover */
.btn:not(.disabled):hover { background: royalblue; }
/* (0,2,0): loses to the hover rule above, even though it comes later */
.btn.primary:hover { background: crimson; }
</style>
</head>
<body>
<button class="btn primary">Hover me</button>
</body>
</html>Hovering the button shows royal blue, not crimson. The :not(.disabled) rule has (0,3,0) and the primary rule has (0,2,0), so source order never comes into play. There are two ways to fix it:
/* Option 1: raise the override to match */
.btn.primary:not(.disabled):hover { background: crimson; }
/* Option 2: keep the :not() but make it weightless */
.btn:where(:not(.disabled)):hover { background: royalblue; }Option 2 puts the :not() inside :where(), so the rule falls to (0,2,0). That ties with .btn.primary:hover, and the later rule wins. Choose this when the :not() is a guard condition and should not influence which rule wins.
:has() specificity in practice
:has() is a relational selector, so its argument often describes something else on the page, such as a child or a following sibling. Its specificity follows the same rule:
/* (0,1,1): .card + img */
.card:has(img) {
padding-block: 0;
}
/* (0,2,0): .card + .photo-hero ... no, .card + .photo; see note below */
.card:has(.photo, img) {
padding-block: 0;
}
/* (0,1,0): :where() hides the argument */
.card:has(:where(img, video)) {
border: 0;
}In the second rule, the most specific argument is .photo at (0,1,0), so the total is .card plus .photo at (0,2,0). The argument list picks one winner, so img adds nothing there.
A leading combinator does not add specificity. .card:has(> img) is (0,1,1), the same as .card:has(img). The combinator changes what is matched, not the weight.
Parsing: these two are not forgiving
The earlier chapter showed that :is() and :where() ignore an invalid item in their lists. :not() and :has() do not.
/* Invalid: the whole rule is dropped, so nothing is styled */
a:not(.external, :unsupported-pseudo) {
color: green;
}
/* Valid: the unknown item is ignored and links still match */
a:is(:not(.external), :unsupported-pseudo) {
color: green;
}In the first rule, the unknown pseudo-class invalidates the entire :not(), and with it the whole rule. If you need fallback behavior for a selector that older browsers may not understand, put it inside :is() or :where().
Other limits apply to both:
- Pseudo-elements do not belong in the argument list of either pseudo-class.
:has()cannot be nested inside another:has().:not()and:has()take specificity from the argument, so an:is(#id)hidden inside either one still counts as an ID.
Choosing a style
Use these checks when you write a selector with :not() or :has().
- List the arguments and find the most specific one. That is what you pay.
- If the condition is only a guard, move it inside
:where()so it costs nothing. - Prefer one
:not(a, b)over:not(a):not(b)unless you want the extra weight. - If you need to tolerate unknown selectors, wrap the argument in
:is().
Exercise
What are the specificities of nav a:not(.current, #skip) and nav a:not(.current):not(#skip)? Which one can a later rule nav a#skip override only by being more specific, and why?
Show answer
The first is (1,0,2): nav and a add two types, and the list contributes only its most specific argument, #skip (1,0,0). The second is (1,1,2): the chained :not() calls add #skip and .current separately. Neither selector matches an element with #skip, so nav a#skip never competes with them for the same element. For other links, the chained form is (1,1,2) and the list form is (1,0,2), so a later rule with (1,0,2) can override only the list form by coming later in source order. It cannot override the chained form without more weight.
Exercise
A rule .panel:has(> .alert):not(.dismissed) { border-color: red; } keeps beating your later .panel.quiet { border-color: gray; }. Compute both specificities and rewrite the first rule so the later one can win without increasing the second rule's specificity.
Show answer
The first rule is .panel (0,1,0) plus :has(> .alert) (0,1,0) plus :not(.dismissed) (0,1,0), giving (0,3,0). The second is (0,2,0), so it loses. Rewrite the first as .panel:where(:has(> .alert):not(.dismissed)) { border-color: red; }, which is (0,1,0). Then .panel.quiet at (0,2,0) wins.