Sub topic 3
Matching descendants and direct children
This chapter assumes you know type, class, and attribute selectors, the descendant (space) and child (>) combinators, and basic specificity. It also assumes you have read the previous chapter on the :has() syntax.
The default test is "anywhere inside"
:has() takes a relative selector. With no combinator at the start, it means "any descendant". The selector matches the element if at least one descendant, at any depth, matches the argument.
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}Here is markup to test it with:
<div class="card">
<p>Text only card</p>
</div>
<div class="card">
<div class="media">
<img src="photo.jpg" alt="A mountain lake">
</div>
<p>Card with an image</p>
</div>The first card keeps its normal layout. The second becomes a two-column grid. Its img is a grandchild, not a child, and it still counts. The styled element is the .card, not the image. That is the point of :has(): the parent reacts to its contents.
Restricting the match to direct children
Start the argument with > to test only direct children.
figure:has(> figcaption) {
border-bottom: 2px solid currentColor;
}This matches a figure whose own child is a figcaption. A figure that merely contains a nested figcaption deeper inside, for example inside a blockquote, does not match.
Without the >, the selector would be figure:has(figcaption). That matches both cases.
Choosing between the two forms
| Selector | Matches when | Typical use |
|---|---|---|
| `.card:has(img)` | An `img` exists at any depth | The content can be wrapped in other elements |
| `.card:has(> img)` | An `img` is a direct child | The structure is fixed and you want precision |
| `.card:has(> .media img)` | An `img` sits inside a direct child `.media` | A specific path through the markup |
Use the direct-child form when a looser match could catch content from a nested component. A card containing another card would otherwise match because of the inner card's image.
Common errors
- Expecting the image to be styled. The subject of the selector is the element left of
:has(). To style the image too, write a second rule such as.card:has(img) img. - Forgetting the nested case.
.card:has(img)matches an outer card whose only image belongs to an inner card. If that is wrong for your layout, use>or a path like:has(> .media img). - Putting
>in the wrong place..card > :has(img)selects a child of the card that contains an image..card:has(> img)selects the card itself. The two are different selectors. - Putting a combinator after the argument.
:has(img >)is invalid. A relative selector starts with a combinator or a compound selector. It never ends with one.
Trying it
You can check a match in the browser console before writing any CSS:
document.querySelectorAll('.card:has(> img)').length;Compare that count with .card:has(img). The difference is the number of cards where the image is wrapped.
Exercise
A page has <section><div><h2>Title</h2></div></section>. Which of these selectors match the section: section:has(h2), section:has(> h2), section:has(> div > h2)?
Show answer
section:has(h2) matches, because h2 is a descendant at any depth. section:has(> h2) does not match, because h2 is not a direct child of section. section:has(> div > h2) matches, because it follows the path through the direct child div.