Sub topic 10
What :has() cannot do
This chapter assumes you can write :has() with relative selectors, including the >, + and ~ combinators. It also assumes you know basic specificity. Here you will learn where :has() stops working and how to rewrite selectors that hit those limits.
Restriction 1: no nesting
A :has() cannot appear inside another :has(). The browser treats the whole selector as invalid and drops the rule.
/* Invalid: :has() inside :has() */
.card:has(.body:has(img)) {
display: grid;
}You can usually flatten the selector, because :has() already matches descendants at any depth.
/* Valid: the card contains an img inside .body */
.card:has(.body img) {
display: grid;
}Other pseudo-classes can wrap or sit inside :has(). These are valid:
.card:not(:has(img)) {
padding: 2rem;
}
.card:has(:not(img)) {
border: 1px solid #ccc;
}The ban is only on :has() inside :has(). This includes indirect cases such as :has(:is(:has(img))).
Restriction 2: no pseudo-elements
Pseudo-elements such as ::before, ::after and ::first-line are not valid inside :has(). They are not elements in the DOM, so there is nothing for the selector to test.
/* Invalid: pseudo-element inside :has() */
.card:has(::before) {
color: red;
}Style the pseudo-element from the element that does match:
/* Valid: :has() picks the element, ::before styles its generated box */
.card:has(img)::before {
content: "Photo";
}Restriction 3: one bad argument breaks the list
Some selector lists, such as :is(), ignore an invalid entry and keep the rest. :has() does not. If any selector in its list is invalid or unsupported, the entire rule is discarded.
/* The whole rule is dropped because ::before is invalid */
.card:has(img, ::before) {
display: grid;
}This matters when you mix selectors that browsers support unevenly. Keep each :has() condition in its own rule, so one failure does not take down the others.
Restriction 4: it only sees elements
:has() tests the element tree and element state. It cannot test:
- Text content. There is no
:has("Sale"). - Text nodes.
<p>Hello</p>and<p></p>differ only in text, so:has()cannot tell them apart. Use:emptyfor elements with no children at all. - Computed styles. You cannot ask for a parent whose child has
display: none. - Whether a child is visible. A hidden element in the DOM still matches.
The workaround is to expose the fact you care about as an attribute or class, then match that:
.card:has([data-badge="sale"]) {
border-color: crimson;
}Restriction 5: it cannot change the match you already have
:has() filters an element by what is around it. It does not move the selection. In .card:has(img), the styled element is always .card, never the img. To style the image, add a descendant selector after the :has() part:
.card:has(img) img {
border-radius: 8px;
}Summary of limits
| You want to | Allowed | Alternative |
|---|---|---|
| Put `:has()` inside `:has()` | No | Flatten with a descendant selector |
| Use `::before` inside `:has()` | No | Put the pseudo-element after the `:has()` |
| Match on text content | No | Add a `data-` attribute or class |
| Put `:not()` or `:is()` inside `:has()` | Yes | None needed |
| Put `:has()` inside `:not()` | Yes | None needed |
| Keep valid entries when one is invalid | No | Use separate rules |
Exercise
This rule does nothing. Explain why, then rewrite it so it works: .list:has(li:has(a.active)) { background: #eef; }
Show answer
It nests :has() inside :has(), which is invalid, so the browser drops the rule. Flatten it, because :has() matches descendants at any depth: .list:has(li a.active) { background: #eef; }. If a.active must be a direct child of an li, write .list:has(li > a.active).
Exercise
You want to color a card red when its title text is "Sale". Why can't :has() do this, and what is the fix?
Show answer
:has() cannot match text content. Add an attribute such as data-badge="sale" to the card or title, then use .card:has([data-badge="sale"]) or match the attribute directly.