Sub topic 10
What :has() cannot do
This chapter assumes you can write :has() selectors with descendant, child and sibling arguments, and that you know how :not() and :is() combine with them. Here we cover the limits of :has(): what the parser rejects, what it silently ignores, and what the selector cannot observe at all. Knowing these limits saves you from debugging rules that look right but never apply.
The subject is always the anchor
The element on the left of :has() is the anchor, and it is the only element the rule styles. The argument inside the parentheses only decides whether the anchor matches. This is the most common source of confusion.
/* Styles the .card, not the img */
.card:has(img) {
border: 1px solid #ccc;
}To style the image as well, write a second rule. It can use the same condition.
.card:has(img) img {
border-radius: 0.5rem;
}The second rule repeats part of the condition, and that repetition is normal. :has() has no way to name a matched element from inside the argument and style it from there.
No nesting of :has()
You cannot put a :has() inside another :has(). The selector is invalid, so the browser drops the whole rule.
/* Invalid: :has() inside :has() */
.card:has(.body:has(img)) {
padding: 2rem;
}The restriction applies at any depth of the argument. Wrapping the inner :has() in :not() or :is() does not make it valid. That is why the following is also invalid.
/* Invalid: the inner :has() is still inside the argument */
.card:has(:not(:has(img))) {
padding: 2rem;
}Most nested cases can be rewritten by flattening the condition into a descendant path:
/* Valid: "a .card containing a .body that contains an img" */
.card:has(.body img) {
padding: 2rem;
}If you need two independent conditions on the anchor, chain two :has() calls on the same compound selector. Chaining is allowed. Only nesting is not.
/* Valid: contains an img AND contains a figcaption */
.card:has(img):has(figcaption) {
display: grid;
}
/* Valid: contains an img but no video */
.card:has(img):not(:has(video)) {
display: grid;
}Each :has() in a chain tests the same anchor. The two conditions can be satisfied by different descendants, so .card:has(img):has(figcaption) matches a card whose image and caption are unrelated elements. If they must be related, put them in one path, such as .card:has(figure > img).
CSS nesting, the & syntax, is a different feature and works with :has(). This is valid:
.card {
padding: 1rem;
&:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
}
}That is just .card:has(img) written in nested form. It does not put one :has() inside another.
No pseudo-elements in the argument
Pseudo-elements such as ::before, ::after, ::first-line and ::marker cannot appear inside :has(). Pseudo-elements are not DOM elements. They exist only in the rendering tree, so the selector engine has nothing to test.
/* Invalid: pseudo-element inside the argument */
.badge:has(::before) {
color: red;
}
/* Invalid: pseudo-element on a descendant */
.card:has(p::first-line) {
color: red;
}A pseudo-element after the :has() is fine, because it describes the part of the anchor you want to style:
/* Valid: style the anchor's ::before when the anchor has an img */
.card:has(img)::before {
content: "Illustrated";
font-size: 0.75rem;
}This means you cannot ask "does this element have generated content?" or "is this text on its first line?". If the condition depends on something a pseudo-element produces, move that fact into the markup. A class or a data- attribute that you can test with :has([data-kind="illustrated"]) is the usual way.
Some pseudo-classes are also affected. Pseudo-classes that depend on user interaction state work normally inside :has(), so :has(:hover), :has(:focus-visible) and :has(:checked) are all valid. :visited is the exception, covered next.
One bad argument invalidates the whole rule
A selector list inside :has() is not forgiving. If one entry is invalid, the browser discards the entire rule, not just that entry. This differs from :is() and :where(), which skip invalid entries and keep the valid ones.
/* The ::before entry is invalid, so this whole rule is dropped,
including the valid .icon entry */
.card:has(.icon, ::before) {
gap: 1rem;
}The failure is silent, with no error and no partial match. When a :has() rule does nothing, check the argument for a nested :has(), a pseudo-element, or a typo before you suspect the cascade. If you want one list to survive a bad entry, split it into separate rules or wrap the pieces in :is() outside the :has().
An empty argument is also invalid. .card:has() matches nothing and invalidates the rule.
:visited never matches inside :has()
For privacy, browsers limit what CSS can reveal about browsing history. A link that is visited inside :has() is treated as unvisited, so a rule like this never applies:
/* Never matches, even if the link has been visited */
li:has(a:visited) {
opacity: 0.5;
}If you need to dim items for visited links, style the link itself with a:visited. You are limited to the properties browsers allow there, mostly colors.
Things the selector engine cannot see
A selector can only test what the DOM and the browser's state expose. Four things are out of reach.
- Text content. There is no
:has(text)or "contains the word" test. You can match attributes, such as[lang="fr"], and the:emptystate, but not what a paragraph says. Put a marker attribute in the markup, or use JavaScript. - Computed styles. A selector cannot ask whether a child is
display: noneor wider than 300 pixels. Selectors read the DOM and state, not the results of layout. - Ancestors of the anchor. The argument is relative to the anchor and looks downward and forward (descendants, children, following siblings). There is no argument that looks at the anchor's parent. If you need "an element whose parent has class
x", write.x > .el. If you need the parent styled, make the parent the anchor. - Shadow trees. Selectors are scoped to one tree. A rule in the page's stylesheet cannot match elements inside a web component's shadow root, so
my-widget:has(.inner)does not see an.innerthat lives in the component's shadow DOM. Elements you pass in as light DOM children are visible.
The table summarizes the limits and the usual workaround.
| You want | Why it fails | Workaround |
|---|---|---|
| `:has()` inside `:has()` | Nesting is invalid | Flatten into one path, or chain `:has()` on the anchor |
| `:has(::before)` | Pseudo-elements are not in the DOM | Add a class or `data-` attribute in the markup |
| Skip one bad entry in the list | The list is unforgiving | Split into separate rules |
| `:has(a:visited)` | Privacy rule | Style `a:visited` directly |
| Match by text | Selectors do not see text | Add an attribute, or use JavaScript |
| Match by computed size | Selectors do not see layout | Use container queries or JavaScript |
| Style the matched child | The anchor is the subject | Add a second rule that selects the child |
Detecting the problem in practice
Browsers do not warn about an invalid selector in a stylesheet, but you can test one from JavaScript. CSS.supports() with the selector() function returns false for a selector the browser cannot parse.
console.log(CSS.supports('selector(.card:has(img))'));
console.log(CSS.supports('selector(.card:has(.body:has(img)))'));
console.log(CSS.supports('selector(.card:has(::before))'));In a browser that supports :has(), this prints true, then false, then false. The same check works in @supports selector(...) for CSS-only feature detection, which a later chapter covers. Pasting a suspect selector into the console this way is a fast way to tell a syntax problem from a cascade problem.
Exercise
A teammate writes .list:has(li:has(img)) { columns: 2; } and the rule never applies. Explain why, and rewrite it so it matches a .list that contains an li with an img inside.
Show answer
:has() cannot be nested inside another :has(), so the selector is invalid and the browser drops the whole rule. Flatten the argument into a descendant path: .list:has(li img) { columns: 2; }. This matches a .list that contains an img inside any li.
Exercise
You want to give .card elements a red border when they contain an img but no video. Write the selector, and say why .card:has(img):not(:has(video)) is allowed even though :has() appears twice.
Show answer
Use .card:has(img):not(:has(video)) { border: 1px solid red; }. It is allowed because the two :has() calls are chained on the same anchor. Neither is inside the other's argument, and :not(:has(video)) has :has() as a child of :not(), not of another :has().
Exercise
Which of these selectors are valid? (a) a:has(::after) (b) .card:has(img)::after (c) li:has(a:visited) (d) .card:has()
Show answer
Only (b) is valid and useful. (a) is invalid because pseudo-elements cannot appear inside :has(). (c) parses, but :visited never matches inside :has(), so it never applies. (d) is invalid because the argument is empty.