Sub topic 5
How specific is :has()?
This chapter assumes you know how specificity is calculated for type, class, and ID selectors, and how the cascade picks a winner. It covers how :has() fits into that calculation, which is simpler than it first looks.
The rule
The :has() pseudo-class adds no specificity of its own. Its specificity is the specificity of the most specific selector in its argument list. That value is added to the rest of the compound selector.
Specificity is written as three numbers: (IDs, classes, types). Classes, attributes, and pseudo-classes count in the middle column. Type selectors count in the last.
/* (id, class, type) specificity */
.a:has(#x) { } /* (1,1,0): the :has() argument's ID counts */
.a:has(.b, p) { } /* (0,2,0): the most specific argument in the list counts */In the first rule, .a contributes (0,1,0) and #x contributes (1,0,0). The total is (1,1,0). In the second rule, .b is (0,1,0) and p is (0,0,1). Only the larger one counts, so the total is (0,2,0).
:not() and :is() use the same rule. This means you can reason about all three in one way.
Worked calculations
Complex selectors inside :has() count in full. A combinator adds nothing, but every compound selector in the argument adds its parts.
| Selector | Argument value | Total |
|---|---|---|
| `.card:has(img)` | (0,0,1) | (0,1,1) |
| `.card:has(> figure img)` | (0,0,2) | (0,1,2) |
| `.card:has(img.hero)` | (0,1,1) | (0,2,1) |
| `.card:has(img, #hero)` | (1,0,0) | (1,1,0) |
| `h2:has(+ p)` | (0,0,1) | (0,0,2) |
The leading >, +, or ~ inside the argument does not change the number. Only the selectors on either side of it do.
The most specific argument always counts
The browser takes the maximum of the list. It does this whether or not that argument matches the element.
.card:has(.thumb, #hero) {
padding: 0;
}This rule has specificity (1,1,0). It stays at that value even on a card that contains only .thumb and no #hero. A single ID in the list makes the whole rule hard to override later.
.card:has(img) {
padding: 0; /* (0,1,1) */
}
.card.featured {
padding: 2rem; /* (0,2,0) */
}If a card has the featured class and also contains an image, .card.featured wins. Its middle column is 2 and the other rule's is 1. Source order does not matter here, because the specificity values differ.
Skip that stub. Here is the decision flow as a proper diagram.
Keeping specificity low with :where()
Sometimes you want a content-aware rule that is easy to override. Wrap the :has() condition in :where(), which always has zero specificity.
.card:where(:has(img)) {
display: grid; /* (0,1,0): only .card counts */
}
.card:has(img) {
display: grid; /* (0,1,1): img counts */
}The first version is weaker by one type selector. Use it for defaults inside a library or reset, where authors should be able to override it with a plain class.
Common mistakes
- Counting
:has()as a class. It is not counted. Only its argument is. - Adding every argument together. Only the single most specific argument counts, not the sum of the list.
- Ignoring nested pseudo-classes.
.a:has(:not(.b))is (0,2,0), because:not()takes the specificity of.b. - Putting an ID in the argument. An ID inside
:has()raises the whole rule to the ID column. Prefer classes.
Exercise
What is the specificity of ul.menu:has(> li.active, a[href]), written as (ids, classes, types)? Which argument decides it?
Show answer
The result is (0,3,1). ul.menu contributes (0,1,1). The argument > li.active is (0,1,1) and a[href] is also (0,1,1), so the most specific argument is (0,1,1). Adding it to (0,1,1) gives (0,2,2). Correction: the sum is (0,2,2). Both arguments tie at (0,1,1), and the leading combinator adds nothing.