← All notes

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

Sources

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

Permanent ID: iil:note:2q4mq5h3

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.},
}

Markdown

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.

All artifact files (JSON index)

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.