The :has() pseudo-class: style an element from what it contains
Answer: The :has() pseudo-class styles an element from what it contains. In a simple case it acts as a parent selector. It can be combined with other selectors. Nesting :has() inside :has() is not allowed.
A parent selector, and more
Style an element from the content it contains by using :has().
button:has(svg) targets buttons that include icons.
In its most basic configuration, you can think of :has() as a parent selector.
Use the matching parent selector combined with other selectors to target other elements.
The :has() pseudo-class cannot be nested inside another :has(), but it can be combined with other pseudo-classes.
A selector list is unforgiving: one invalid entry means every rule in that list is ignored.
Do not put a pseudo-element in the :has() list.
The subject can sit at the front
:has() is more than a parent selector.
Major browsers all shipped it at the end of 2023.
Usually the subject of a selector is at the end, like ul li.
With :has(), the first part of the selector can be the subject instead.
It can keep the container as the subject while querying the number of children.
Some uses check for something based on a common ancestor, then pivot the subject to a child.
See also
Claims
- id: C1 claim: Use :has() to style elements based on what they contain. source: S1 evidence: "style elements based on what is contained"
- id: C2 claim: :has() cannot be nested inside another :has(). source: S1 evidence: "cannot be nested inside of another"
- id: C3 claim: It can be combined with other pseudo-classes. source: S1 evidence: "combined with other pseudo-classes"
- id: C4 claim: :has() is more than a parent selector. source: S2 evidence: "more than a \"parent selector\""
- id: C5 claim: With :has(), the first element in the selector can be the subject. source: S2 evidence: "element at the beginning of the selector can become"
- id: C6 claim: The selector became available in all major browsers at the end of 2023. source: S2 evidence: "landed across all major browsers at the end 2023"
Sources
- [S1] Pseudo-classes. web.dev. https://web.dev/learn/css/pseudo-classes . License: CC-BY-4.0. Accessed 2026-11-23.
- [S2] 5 CSS snippets every front-end developer should know in 2024. web.dev. https://web.dev/articles/5-css-snippets-every-front-end-developer-should-know-in-2024 . License: CC-BY-4.0. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: web.dev, Pseudo-classes; web.dev, 5 CSS snippets every front-end developer should know in 2024. CC-BY-4.0 (https://web.dev/learn/css/flexbox). Copyright Google LLC. web.dev text is CC BY 4.0. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: web.dev, Pseudo-classes; web.dev, 5 CSS snippets every front-end developer should know in 2024. CC-BY-4.0 (https://web.dev/learn/css/flexbox). Copyright Google LLC. web.dev text is CC BY 4.0.
Cite this
iinventedlight. 2026. "The :has() pseudo-class: style an element from what it contains." iinventedlight Library, iil:note:2q4mq5h3, version of 2026-11-23. https://iinventedlight.com/library/notes/css-has-selector/. License: CC-BY-4.0.
@misc{iil_note_2q4mq5h3,
author = {{iinventedlight}},
title = {{The :has() pseudo-class: style an element from what it contains}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/css-has-selector/},
note = {Permanent ID iil:note:2q4mq5h3. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- css-has-selector.schema.json (schema; CC-BY-4.0)
- css-has-selector.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/css-has-selector.md.