Sub topic 1
Styling a Parent from Its Children: What :has() Does
Prerequisites
This chapter assumes you already know:
- Basic selectors: type, class, ID, and attribute.
- The four combinators: descendant (space), child (
>), adjacent sibling (+), and general sibling (~). - Pseudo-classes such as
:hover,:checked, and:nth-child(). - How the cascade and specificity decide which rule wins, at a basic level.
The problem :has() solves
Until recently, CSS selectors could only look down the tree or forward along siblings. The selector .card img matches an image inside a card. No selector could match the card itself because it contains an image.
The usual workaround was JavaScript. A script found the cards with images and added a class such as card--with-image. That meant extra code, and the class could go stale when the markup changed.
What :has() matches
:has() is a relational pseudo-class. In A:has(B), the selector matches element A if the relative selector B matches at least one element, with A as the starting point.
The element that gets styled is A, the one before :has(). The contents of B only act as a test.
Here is a small example. Given this markup:
<div class="card">
<img src="photo.jpg" alt="A mountain lake">
<p>Lake Louise at sunrise.</p>
</div>
<div class="card">
<p>A card with text only.</p>
</div>this rule gives the first card a two-column layout and leaves the second alone:
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}The img does not have to be a direct child. Without a combinator, :has() tests all descendants at any depth.
Reading the argument
The argument of :has() is a relative selector. It may begin with a combinator, and that combinator is read relative to the anchor element. Without a leading combinator, the default is the descendant combinator.
| Selector | Matches |
|---|---|
| `.card:has(img)` | A `.card` with an `img` anywhere inside it |
| `figure:has(> figcaption)` | A `figure` whose direct child is a `figcaption` |
| `h2:has(+ p)` | An `h2` immediately followed by a `p` |
The second rule below shows the child combinator in use:
figure:has(> figcaption) {
border-bottom: 2px solid currentColor;
}A figure with a caption nested deeper does not match, because > restricts the test to direct children. The sibling forms in the third row are covered in a later chapter.
Common errors
- Styling the wrong element. In
.card:has(img) { border: 0 }, the border change applies to the card, not the image. To style the image, write a separate rule such as.card:has(img) img. - Forgetting the depth.
.card:has(img)matches an image nested five levels down. Use.card:has(> img)when only a direct child should count. - Treating it as a loop.
:has()is a test on one element. It does not select the elements it finds.
Why it matters
:has() moves decisions from JavaScript back into CSS. The stylesheet can react to what the markup contains, so no helper classes are needed. The rest of this tutorial builds on this idea. It covers sibling matching, specificity, state-based styling such as :checked and :focus-visible, and patterns for cards, forms, and page-level toggles.
Exercise
Given <section><div><h3>Title</h3></div></section>, does section:has(> h3) match the section? Does section:has(h3) match it? Which element would be styled in each case?
Show answer
section:has(> h3) does not match, because the h3 is a child of the div, not a direct child of the section. section:has(h3) matches, because the descendant check reaches any depth. In both cases the styled element is the section, the element before :has(), and never the h3.