Sub topic 6
Combining :has() with :not() and :is()
This chapter assumes you can read :has() with a relative selector list, such as .card:has(> img). You should also know what :not() and :is() do on their own and how specificity is counted. Here you will combine them, and the main skill is working out which combination expresses the condition you mean.
Why combine them
:has() answers the question "does this element contain or precede something?". Real styling rules usually need more than that, such as:
- Does it have no image?
- Does it have a heading of any level?
- Does it have an invalid field but no focused one?
:not() handles negation. :is() handles grouping. Both can sit inside :has(), and :has() can sit inside both. The nesting order changes the meaning, and the difference is easy to miss.
:not(:has()): the element lacks something
:not(:has(img)) reads as "not an element that has an img". The :has() test runs first. The :not() then inverts the result.
.card:not(:has(img)) {
padding: 2rem;
}This matches every .card that has no img at any depth. It is the usual way to give a "plain" variant of a component its own styles without adding a class in the markup.
:has(:not()): the element contains something else
The reverse nesting means something different. :has(:not(img)) reads as "has at least one descendant that is not an img".
.card:has(:not(img)) {
border: 1px solid #ccc;
}Consider three cards:
<div class="card"><img src="a.jpg" alt="A"><p>Text</p></div>
<div class="card"><p>Text only</p></div>
<div class="card"><img src="b.jpg" alt="B"></div>| Selector | Card 1 (img + p) | Card 2 (p only) | Card 3 (img only) |
|---|---|---|---|
| `.card:not(:has(img))` | no | yes | no |
| `.card:has(:not(img))` | yes | yes | no |
| `.card:has(img)` | yes | no | yes |
Card 1 matches :has(:not(img)) because the p is a descendant that is not an img. The presence of an image does not stop it. The rule does not mean "has no images".
Text nodes do not count. A card containing only a text string and no child elements matches neither :has(img) nor :has(:not(img)), because both tests look at element descendants.
Grouping with :is()
:is() takes a selector list and matches if any item matches. Inside :has(), it shortens repeated arguments.
/* These two selectors are equivalent */
.card:has(:is(h2, h3)) {
gap: 0.5rem;
}
.card:has(h2, h3) {
gap: 0.5rem;
}:has() already accepts a list, so :is() is optional there. It becomes useful when a relative combinator applies to a group:
.card:has(> :is(h2, h3, h4)) {
padding-top: 0;
}It is also useful on the outside, when the same :has() test applies to several kinds of container:
:is(.card, .panel, .tile):has(img) {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}Without :is(), you would repeat :has(img) three times. You can also group the subject of the condition with elements that need different tags, such as :is(ul, ol):not(:has(> li)). That selector matches any list with no direct list item.
OR, AND, and neither
A selector list inside :has() means "or". To express "and", chain several :has() pseudo-classes on the same element. Wrapping in :not() gives the negated forms.
| Condition | Selector |
|---|---|
| Has an img or a video | `.card:has(img, video)` |
| Has an img and a figcaption | `.card:has(img):has(figcaption)` |
| Has neither an img nor a video | `.card:not(:has(img, video))` |
| Does not have both an img and a figcaption | `.card:not(:has(img):has(figcaption))` |
The third row is equivalent to .card:not(:has(img)):not(:has(video)). The fourth row passes cards with only one of the two, and cards with neither. If you want "has an img but no figcaption", chain the two parts:
.card:has(img):not(:has(figcaption)) {
border-style: dashed;
}Specificity of the combination
:is(), :not(), and :has() all take the specificity of their most specific argument. :where() always counts as zero. The wrapper itself adds nothing.
.card:not(:has(img)) { } /* (0,1,1): .card plus the img type selector */
.card:not(:has(#hero)) { } /* (1,1,0): the ID inside counts */
:is(.card, #page):has(img) { } /* (1,0,1): the most specific item in :is() counts */
.card:has(:where(img, video)) { } /* (0,1,0): :where() contributes nothing */The :is() case surprises people. The ID #page raises the specificity even for an element that matched through .card. The browser does not track which branch matched. If a group should not raise specificity, put it inside :where().
Validity rules that affect combinations
Three rules determine whether a combined selector is accepted at all.
:has()cannot contain another:has(), at any depth..a:has(:is(:has(b)))and.a:has(:not(:has(b)))are both invalid.:has()cannot contain pseudo-elements, so.card:has(img::before)is invalid.:has()and:not()are unforgiving. One invalid item in the list makes the whole rule drop.:is()and:where()are forgiving. They ignore an invalid item and keep the valid ones.
The valid nesting orders are therefore :not(:has()), :is(:has()), :has(:not()), and :has(:is()). Nesting :has() inside :has() is the only forbidden one.
You can often avoid the nesting restriction by moving the second condition outside. Instead of "an element that has a section that has an h2", which would need :has(section:has(h2)), chain a descendant selector: :has(section h2) already finds an h2 inside a section inside the element.
A worked example: form submit state
This pattern combines :is() for grouping field types, :has() for detecting a problem, and :not() for the opposite state.
<form class="signup">
<label>Email <input type="email" required></label>
<label>Plan
<select required>
<option value="">Choose</option>
<option value="free">Free</option>
</select>
</label>
<button type="submit">Create account</button>
</form>.signup button {
opacity: 1;
}
.signup:has(:is(input, select, textarea):invalid) button {
opacity: 0.5;
cursor: not-allowed;
}
.signup:not(:has(:is(input, select, textarea):invalid)) button {
outline: 2px solid seagreen;
}The empty required fields are :invalid, so the first conditional rule dims the button. Once every field is valid, the :not(:has(...)) rule takes over and outlines the button. Both conditional rules use the same inner group, so a reader can see that they are opposites.
The second rule has specificity (0,2,2): the class .signup and the button type, plus the input type from the :is() list and the :invalid pseudo-class. This is low enough that later component styles can still override it.
Choosing between the forms
When you write a combined selector, say the condition aloud first.
- "Cards that have no image" is
:not(:has(img)). - "Cards that have something other than an image" is
:has(:not(img)). - "Cards that have a heading of any level" is
:has(:is(h1, h2, h3))or:has(h1, h2, h3). - "Cards that have an image and a caption" is
:has(img):has(figcaption).
If you cannot tell which form you have written, test it against a card containing only the thing you are excluding, as in Card 3 above.
Exercise
Given <div class="box"><span>Hi</span></div> and <div class="box"><img src="x.jpg" alt=""></div>, which boxes does .box:has(:not(img)) match, and which does .box:not(:has(img)) match? Which selector should you use to style boxes that contain no image?
Show answer
.box:has(:not(img)) matches only the first box, because the span is a descendant that is not an img. The second box has only an img, so it fails. .box:not(:has(img)) also matches only the first box here, but for a different reason: it has no img. The two selectors give the same result on this markup only by coincidence. For "boxes with no image", use .box:not(:has(img)). If the first box also contained an img next to the span, :has(:not(img)) would still match it, which is wrong for that goal.
Exercise
Write one selector that matches any ul or ol that has no direct li child, and state why ul:has(:has(li)) would not be a valid alternative form of a related selector.
Show answer
:is(ul, ol):not(:has(> li)). The > limits the test to direct children. :has(:has(li)) is invalid because :has() cannot contain another :has(). The whole rule would be discarded. The valid way to find a descendant li is :has(li).