Sub topic 2
How :has() is written
This chapter assumes you know basic selectors, the four combinators (descendant, >, +, ~), and pseudo-classes such as :hover and :nth-child(). The previous chapter covered what :has() does. This one covers how to write it.
The basic shape
:has() is a pseudo-class that takes an argument in parentheses. The element you attach it to is the subject, the element that gets styled. The argument describes what must be true relative to the subject.
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
gap: 1rem;
}Read this as "a .card that has an img inside it". The .card receives the styles. The img is only a condition and is not styled by this rule.
The argument is a relative selector list
Ordinary selectors, such as the ones in .card img, describe an element by looking up from the target. The argument of :has() is different. It is a relative selector list, which means each selector is evaluated starting from the subject, and it may begin with a combinator.
| Argument | Meaning | Example |
|---|---|---|
| No combinator | Descendant at any depth | `.card:has(img)` |
| `>` | Direct child | `figure:has(> figcaption)` |
| `+` | Immediately following sibling | `h2:has(+ p)` |
| `~` | Any following sibling | `h2:has(~ ul)` |
When you leave the combinator out, the browser assumes the descendant combinator. So :has(img) behaves like :has(* img) measured from the subject, and it matches an img at any depth.
Here is the direct-child form:
figure:has(> figcaption) {
border-bottom: 2px solid currentColor;
}The leading > limits the match to a figcaption that is a direct child of the figure. A figcaption nested deeper inside a div would not count. Sibling combinators in the argument are covered in a later chapter.
Compound and complex arguments
The argument can be as detailed as any selector. It can include classes, attributes, pseudo-classes, and several combinators in a chain:
.card:has(.thumbnail[alt]) {
border-color: steelblue;
}
section:has(> header > h2) {
padding-block-start: 0;
}The first rule requires a .thumbnail that has an alt attribute. The second requires a header as a direct child of the section, with an h2 as a direct child of that header. The whole chain is a single condition.
Lists mean "or"
Separate selectors with commas to match when any one of them is true:
.card:has(img, video) {
background: #f6f6f6;
}This matches a card with an img, a video, or both. To require both, chain two :has() calls instead:
.card:has(img):has(video) {
background: #eaeaea;
}Each :has() is an independent test on the same subject, so the subject must pass every one.
Where it can sit in a selector
You can attach :has() to any compound selector, including the universal selector and :root. Two forms are common:
ul:has(li)styles a specific kind of element.:has(img)with nothing in front is the same as*:has(img). It matches every element that contains an image, so you rarely want it.
Being specific about the subject also helps performance, which a later chapter covers.
Two syntax limits to remember now
:has()cannot be nested inside another:has().- The argument cannot target a pseudo-element, such as
::before.
A selector that breaks these rules is invalid, and the browser drops the whole rule. The chapter on restrictions explains why.
Exercise
Write a selector that matches a li whose direct child is an a element, and then a second selector that matches a li containing an a at any depth. What single character separates them?
Show answer
Direct child: li:has(> a). Any depth: li:has(a). The > at the start of the argument is what separates them.
Exercise
What is the difference between .card:has(img, video) and .card:has(img):has(video)?
Show answer
The first matches a card with an img or a video (either one is enough). The second matches only a card that has both, because each :has() is a separate test and both must pass.