Sub topic 5
How specific is :has()?
This chapter explains how browsers compute the specificity of a selector that contains :has(). It assumes you know how specificity works for type, class, and ID selectors, that you can read a specificity triple such as (0,1,1), and that you know the basic :has() syntax from the earlier chapters.
The rule in one sentence
The :has() pseudo-class adds no specificity of its own. Its specificity is the specificity of the most specific selector in its argument list. The browser adds that value to the rest of the compound selector.
The argument list is a list of relative selectors, such as img, > figcaption, or + p. The combinators (>, +, ~, and the descendant space) never add specificity. Only the compound selectors inside the argument count.
Specificity is written as three numbers, (ids, classes, types):
- IDs count in the first position.
- Classes, attribute selectors, and pseudo-classes count in the second.
- Type selectors count in the third. Pseudo-elements also count here, but
:has()forbids them in its argument.
/* (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, .a contributes (0,1,0). The arguments .b (0,1,0) and p (0,0,1) are compared, and .b wins, so the total is (0,2,0).
Worked examples
The table below shows how common selectors from this tutorial are calculated. Read the "Counted from" column to see where each number comes from.
| Selector | Specificity | Counted from |
|---|---|---|
| `.card:has(img)` | (0,1,1) | `.card` is a class. `img` is a type. |
| `.card:has(.hero)` | (0,2,0) | Two classes. |
| `.card:has(#hero)` | (1,1,0) | One ID and one class. |
| `.card:has(> img)` | (0,1,1) | The `>` adds nothing. |
| `h2:has(+ p)` | (0,0,2) | `h2` and `p` are types. The `+` adds nothing. |
| `.card:has(.a, #b)` | (1,1,0) | The ID argument is the most specific one. |
| `.field:has(input:focus-visible)` | (0,2,1) | `.field` is a class. `input` is a type. `:focus-visible` is a pseudo-class. |
| `:root:has(#dark-toggle:checked)` | (1,2,0) | `:root` is a pseudo-class. `#dark-toggle` is an ID. `:checked` is a pseudo-class. |
The last row matters for the theme toggle pattern. A rule that starts from :root:has(#dark-toggle:checked) carries an ID. It beats ordinary class-based rules, even when those rules appear later in the stylesheet.
Only the most specific argument counts
A list does not add its arguments together. This selector has three arguments:
.card:has(.hero, .banner, p) {
border-color: tomato;
}The arguments have specificities (0,1,0), (0,1,0), and (0,0,1). The most specific one is (0,1,0). The total is .card (0,1,0) plus (0,1,0), which is (0,2,0). The extra .banner and p add nothing.
The matched argument does not matter
The browser does not check which argument actually matched an element. It always uses the most specific argument in the list. Consider this rule:
.card:has(#promo, img) {
background: lightyellow;
}Its specificity is always (1,1,0). That holds even for a card that contains only an img and no #promo. This differs from how some people expect it to work. An ID in the list raises the weight of the whole rule for every element it matches.
Because of this, keep unrelated arguments in separate rules when you want them to have different weights. Do this when one of them carries an ID or other heavy selector.
.card:has(img) {
background: lightyellow;
}
.card:has(#promo) {
background: gold;
}Here the img rule stays at (0,1,1), and only the #promo rule is (1,1,0).
How nested pseudo-classes count
Pseudo-classes inside :has() follow their own specificity rules. Three cases come up often:
- A state pseudo-class such as
:checked,:focus-visible, or:invalidcounts as a class: (0,1,0). :not()and:is()take the specificity of their most specific argument, just as:has()does.:where()always counts as zero, no matter what is inside it.
So .card:has(:not(img)) is (0,1,0) for .card plus (0,0,1) for img, which makes (0,1,1). The :not() itself adds nothing, and its argument img is what counts.
Compare the two rules from the chapter on :not():
.card:not(:has(img)) {
padding: 2rem;
}
.card:has(:not(img)) {
border: 1px solid #ccc;
}Both come to (0,1,1). They match different elements, but their weight is the same. If a single card could match both rules and they set the same property, source order would decide.
A conflict worked through
Specificity matters most when a :has() rule competes with a plain class rule. Here is a card that is both featured and contains an image:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Specificity of :has()</title>
<style>
.card:has(img) {
background: lightyellow; /* (0,1,1) */
}
.card.featured {
background: lightblue; /* (0,2,0) */
}
</style>
</head>
<body>
<div class="card featured">
<img src="photo.jpg" alt="A mountain lake">
<p>Featured card with an image.</p>
</div>
</body>
</html>The card matches both rules. The first rule is (0,1,1). The second is (0,2,0). The browser compares the triples from left to right, and the second position decides it: 2 is greater than 1. The card is light blue, even though the :has(img) rule looks more specific because it describes more.
A common mistake is to think a rule is stronger because it needs more conditions to match. Specificity counts selector parts. It does not count how hard the condition is to satisfy.
To make the image rule win, make its selector heavier:
.card.featured:has(img) {
background: lightyellow; /* (0,3,1) */
}The new selector is (0,3,1): two classes from .card.featured, one from the img argument's type, and one more class counted by... no, the count is two classes plus one type, which is (0,2,1). It beats (0,2,0) because the third position is higher.
Recount to be sure. .card is (0,1,0). .featured is (0,1,0). The img argument is (0,0,1). The total is (0,2,1). That is greater than (0,2,0), so the rule wins.
Controlling specificity on purpose
Sometimes you want :has() to add nothing. A rule that depends on an ID inside :has() is hard to override later. Two techniques keep the weight low.
- Wrap the argument in
:where(). The argument then counts as zero:
``css .card:has(:where(#promo, img)) { background: lightyellow; /* (0,1,0) */ } ``
- Wrap the whole
:has()in:where():
``css .card:where(:has(img)) { background: lightyellow; /* (0,1,0) */ } ``
Both versions leave only .card counting. A later .card.featured rule at (0,2,0) now overrides them without trouble. This is a good fit for defaults in a base stylesheet or a shared component library, where consumers need to override your rules easily.
The reverse also works. If you need a rule to win, add a class or an attribute selector inside the arguments, instead of reaching for !important.
The calculation as a flow
The diagram shows the steps the browser follows. Start with the part outside :has(), then add the heaviest argument.
Edge cases to remember
- Combinators add nothing.
h2:has(+ p)andh2:has(p)both have an argument that contributes (0,0,1). - Ties go to source order. When two rules have equal specificity, the one that appears later wins.
- The universal selector adds nothing.
:has(*)contributes (0,0,0), so.card:has(*)is (0,1,0). - Attribute selectors count as classes.
.field:has([required])is (0,2,0). !importantand inline styles sit outside this system. Inline styles beat any selector, including one with:has(). A declaration marked!importantbeats a normal one, whatever the selector.
Exercise
What is the specificity of .field:has(input:invalid:not(:placeholder-shown)) label, written as (ids, classes, types)?
Show answer
(0,3,2). Count .field as (0,1,0). Inside :has(), input is (0,0,1), :invalid is (0,1,0), and :not(:placeholder-shown) takes the specificity of its argument, (0,1,0). The argument totals (0,2,1). The trailing label adds (0,0,1). The sum is (0,1,0) + (0,2,1) + (0,0,1) = (0,3,2).
Exercise
A card contains only an img. Rule A is .card:has(#promo, img) { color: red; }. Rule B is .card.sale { color: blue; }, and the card has both classes. Which color does the card text get, and why? Then rewrite Rule A so Rule B wins.
Show answer
The text is red. Rule A has specificity (1,1,0) because the ID in the argument list counts even though #promo does not match this card. Rule B is (0,2,0). The first position decides, so Rule A wins. To let Rule B win, move the ID out or neutralize it, for example .card:has(:where(#promo), img), which is (0,1,1). Now Rule B at (0,2,0) beats it.