Sub topic 1
Styling a Parent from Its Children: What :has() Does
This chapter introduces :has(), the CSS pseudo-class that lets a rule select an element based on what it contains or what follows it. You will learn what it matches, how to read it, and which mistakes to avoid. Later chapters cover the full syntax, specificity, state, and performance.
Prerequisites
This chapter assumes you already know:
- Basic selectors: type, class, ID, and attribute selectors.
- The four combinators: descendant (a space), child (
>), adjacent sibling (+), and general sibling (~). - Common pseudo-classes such as
:hover,:checked, and:nth-child(). - The cascade and basic specificity, meaning that more specific selectors win over less specific ones.
You do not need any JavaScript for this chapter.
The problem :has() solves
A CSS selector always has a subject, the element that receives the styles. The subject is the rightmost compound selector. In nav a.active, the subject is the a element. The nav only narrows which a elements qualify.
For most of CSS's history, a selector could only look in two directions from an element: up to its ancestors (as in nav a) and back to its earlier siblings (as in h2 + p). It could not test what an element contained or what came after it. So these questions had no CSS answer:
- Which
.cardelements contain an image? - Which
h2is followed by a list? - Which form field wrapper contains an invalid input?
The usual workaround was JavaScript that inspected the DOM and added a class such as has-image to the parent. Another was to have the server or template emit the class. Both approaches duplicate information the markup already holds, and they go stale when the DOM changes.
:has() removes the need for that workaround. It is often called the "parent selector", but the name is too narrow. It can also look at following siblings, which makes it possible to style an element based on what comes after it.
What :has() does
:has() is a functional pseudo-class. It takes a list of relative selectors as its argument. An element matches if at least one of those relative selectors, evaluated with that element as the starting point, matches something in the document.
Start with this markup:
<article class="card">
<img src="photo.jpg" alt="A mountain lake" width="400" height="300">
<h2>Mountain lake</h2>
<p>A short hike from the parking area.</p>
</article>
<article class="card">
<h2>Trail notes</h2>
<p>This card has no image.</p>
</article>Now add this rule:
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}Read it like this: select every .card, but only those that have an img somewhere inside. The first card becomes a two-column grid. The second card is left alone because the condition fails.
The subject of the selector is .card. The :has(img) part is a condition attached to it, in the same way that :hover or :first-child is a condition. The condition happens to be about the element's contents instead of its state or position.
How the browser evaluates it
The diagram shows the decision for each .card element.
The check is live. If a script adds an img to the second card later, the browser re-evaluates the selector and the grid layout starts to apply. If the image is removed, the layout goes away. You do not write any code to keep this in sync.
Why it is called relational
In .card:has(img), the img is not a standalone selector. It is relative to the element being tested, called the anchor. The browser asks: "starting from this .card, can I find an img?"
A relative selector can begin with a combinator. When it does not, the descendant combinator is implied. These two forms behave the same:
.card:has(img) { outline: 1px solid gray; }
.card:has(* img) { outline: 1px solid gray; }The second form is only for illustration. In practice you write the short form. The explicit combinators are what extend :has() beyond descendants:
| Argument starts with | Meaning | Example |
|---|---|---|
| nothing (descendant) | contains a matching element at any depth | `figure:has(img)` |
| `>` | has a matching direct child | `figure:has(> figcaption)` |
| `+` | is immediately followed by a matching sibling | `h2:has(+ p)` |
| `~` | has a matching sibling somewhere after it | `h2:has(~ ul)` |
The next chapters cover each row in detail. For now, notice that the last two rows reach forward to later siblings. Because the subject is the earlier element, :has() lets you style an element based on what comes after it.
Subject versus condition
The most common mistake with :has() is losing track of which element receives the styles. The styles always go to the element immediately before :has(), or to whatever the rest of the selector ends on. Compare these three rules:
/* Styles the card itself */
.card:has(img) {
background: #f4f4f4;
}
/* Styles every p inside a card that has an img */
.card:has(img) p {
color: #333;
}
/* Styles the img, only inside cards that contain an h2 */
.card:has(h2) img {
border-radius: 8px;
}In the second rule, :has(img) is a condition on .card, but the subject is the trailing p. In the third rule, the card must contain an h2, and the img is what gets styled.
Where :has() can sit in a selector
:has() can be attached to any compound selector, and a compound selector can contain more than one :has(). Two examples:
/* A card that contains both an image and a link */
.card:has(img):has(a) {
border: 1px solid #ccc;
}
/* A list item inside a nav that contains a current-page link */
nav li:has(a[aria-current="page"]) {
font-weight: bold;
}Chaining two :has() pseudo-classes means both conditions must hold, so they work like a logical AND. Putting two selectors in one :has(), separated by a comma, is a logical OR. You will use both in later chapters.
A first look at state
The condition inside :has() is not limited to element types. Any selector works, including ones that test state. This small rule highlights a label when the checkbox inside it is checked:
label:has(input:checked) {
background: #e6f2ff;
}The label wraps its checkbox in the markup, so the label is the parent. Before :has(), this effect required JavaScript or a layout where the label came after the input. Here the markup stays natural and the CSS reads like the rule it describes.
Common errors
These are the mistakes learners hit most often in their first hours with :has().
- Expecting a sibling to match without a combinator.
h2:has(p)matches anh2that contains ap. Since anh2rarely contains a paragraph, this matches nothing. To test a following sibling, writeh2:has(+ p)orh2:has(~ p). - Styling the wrong element. Authors write
.card:has(img)when they mean to style the image, or.card imgwhen they mean to style the card. Use the subject check from the tip above. - Writing an empty argument.
.card:has()is invalid. The argument needs at least one relative selector. - Treating it as a replacement for classes.
:has()is the right tool when the condition lives in the content or state of the page. If you already know the condition at authoring time, a class is shorter and cheaper for the browser to evaluate. - Assuming it works in every selector API in the same way. It does work in
querySelector()andmatches(), which a later chapter covers. It does not work in places that accept only a restricted selector syntax.
Two more limits come later in the tutorial: :has() cannot be nested inside another :has(), and it cannot be used with pseudo-elements. You do not need them yet.
Summary of the working model
- A selector's subject is its last compound selector, and
:has()adds a condition to the element before it. - The argument is a list of relative selectors, tested from the anchor element.
- With no leading combinator, the test looks for a descendant at any depth.
- The match is live. It updates as the DOM and the element states change.
Exercise
Given the markup <ul class="menu"><li><a href="/">Home</a></li><li>Plain text</li></ul>, which li elements does li:has(a) match, and which element does .menu:has(a) li style?
Show answer
li:has(a) matches only the first li, because it contains an a. The rule .menu:has(a) li styles both li elements: the condition holds for .menu because it contains an a, and the subject is the trailing li, so every li inside that menu is styled.
Exercise
Write a rule that gives a figure a dashed border only when it contains an img at any depth. Then explain why figure:has(figcaption) and figure:has(> figcaption) can give different results.
Show answer
Use figure:has(img) { border: 1px dashed gray; }. The two figcaption selectors differ because figure:has(figcaption) matches a figure with a figcaption at any depth, including one inside a nested element or inside another figure, while figure:has(> figcaption) matches only when the figcaption is a direct child of that figure.