Sub topic 12
Specificity of :not() and :has()
This chapter assumes you can read specificity as a three-part value (IDs, classes, types), such as (1,0,2). It also assumes you know how :is() and :where() differ in specificity. :not() and :has() follow the :is() rule, so what you learned there carries over.
The rule
The pseudo-class itself adds nothing. Its specificity comes from its argument list, and the most specific argument sets the value. This is the same rule :is() uses.
/* (0,1,1): a is (0,0,1), .external is (0,1,0) */
a:not(.external) {
color: navy;
}
/* (1,0,0): the ID is the most specific argument */
:not(.draft, #archive) {
opacity: 1;
}
/* (0,1,1): .card is (0,1,0), img is (0,0,1) */
.card:has(img) {
padding: 0;
}What counts and what does not
| Selector | Specificity | Reason |
|---|---|---|
| `:not(.a)` | (0,1,0) | The argument `.a` |
| `:not(.a, #b)` | (1,0,0) | `#b` wins, `.a` is ignored |
| `:not(.a):not(#b)` | (1,1,0) | Two separate pseudo-classes add up |
| `:not(:where(.a))` | (0,0,0) | `:where()` contributes nothing |
| `:has(> img)` | (0,0,1) | The combinator adds nothing |
| `:has(.a, #b)` | (1,0,0) | The most specific argument |
Note the difference between :not(.a, #b) and :not(.a):not(#b). Both match the same elements. The list form takes only the highest value. The chained form adds the values together.
:has() and relative selectors
The argument of :has() is a relative selector. It can start with a combinator, such as > img or + p. The combinator does not count toward specificity. Only the compound selectors after it count.
/* (0,0,2): section and h2 are both type selectors */
section:has(> h2) {
margin-top: 2rem;
}
/* (0,1,1): the :has() argument is .warning, which adds (0,1,0) */
section:has(.warning) {
border-color: orange;
}Keeping the specificity low
Both pseudo-classes can raise specificity without you noticing. A single ID in the list raises the whole selector. If you want the matching behavior without the weight, wrap the argument in :where().
/* (0,1,0): :where() hides the ID, so only .card counts */
.card:has(:where(#promo, .badge)) {
border: 1px solid gold;
}This works because :where() is allowed inside :has() and :not(). The outer pseudo-class takes the highest value among its arguments, and :where() contributes zero.
Parsing is not forgiving
:is() and :where() ignore an invalid item in their list. :not() and :has() do not. One invalid argument makes the whole rule invalid, and the browser drops it.
/* The whole rule is dropped if :unsupported-pseudo is unknown */
a:not(.external, :unsupported-pseudo) {
color: navy;
}
/* The invalid item is ignored and .external still matches */
a:not(:is(.external, :unsupported-pseudo)) {
color: navy;
}The second form has a cost. The :is() inside :not() uses its most specific argument, so it is not always a free workaround. Check the resulting value.
Common errors
- Assuming
:not(.a, #b)and:not(.a):not(#b)have the same specificity. They do not. - Putting a pseudo-element inside either argument list. It never matches.
- Expecting
:has()to behave like:where(). It has real specificity. - Writing
:has()with an empty argument. An empty list is invalid.
Exercise
What is the specificity of button:not(.primary, #submit):has(> svg)? Give it as (IDs, classes, types).
Show answer
(1,0,2). button adds one type selector. :not(.primary, #submit) takes its most specific argument, #submit, which adds one ID. :has(> svg) adds one type selector for svg, and the > combinator adds nothing. The types total two.
Exercise
How can you rewrite .card:has(#promo) so that it matches the same elements but has specificity (0,1,0)?
Show answer
Wrap the argument in :where(): .card:has(:where(#promo)). The :where() contributes zero, so only .card counts.