</> HTML5Advent
ENFRESDEITPT

// css · Web Platform Advent #17

The CSS :has() Selector: the Parent Selector, Explained (2026)

The :has() relational pseudo-class lets you style an element based on what it contains or what follows it - the long-awaited parent selector. How it works, real examples, browser support, and the gotchas.

Close-up of colorful CSS code lines on a computer screen

The :has() pseudo-class is the feature CSS authors asked for for two decades: a way to style an element based on what is inside it, or what comes after it. It is usually called the "parent selector," though it is really more powerful than that - it is a relational selector. It now ships in every modern browser, so you can use it in production today.

The basic idea

A normal CSS selector targets an element by its own type, class or state. :has() flips that around: it targets an element because of the elements it contains. You write the parent, then :has(...) with a selector for what must be inside.

/* Style any .card that contains an <img> */
.card:has(img) {
  padding: 0;
}

/* Style a <figure> only when it has a caption */
figure:has(figcaption) {
  border: 1px solid #ddd;
}

The element that actually gets styled is the one before :has() - the .card and the figure, not the image or caption inside. That is the whole trick: the condition is about the descendants, but the target is the ancestor.

It is relational, not just "parent"

Because you can put combinators inside :has(), it does far more than look at children. You can react to siblings, which was impossible in CSS before.

/* A heading that is immediately followed by a paragraph */
h2:has(+ p) {
  margin-bottom: 0.25rem;
}

/* A form that contains an invalid field */
form:has(input:invalid) {
  border-left: 3px solid crimson;
}

/* A label whose related required input is empty and focused */
.field:has(input:required:placeholder-shown) .hint {
  opacity: 1;
}

That second example is the kind of thing people used to reach for JavaScript to do: change the look of a whole form (or any container) based on the state of a control inside it. With :has() it is one line of CSS, and it updates live as the user types.

Close-up view of HTML and CSS code displayed on a screen
:has() reads the relationship between elements, so a container can respond to the content and state inside it - no JavaScript required.

Style the parent if a child has a certain class

This is the question most people arrive with: "can I set CSS on a parent when a child with a given class is present?" Yes. Put the child's class inside :has() and write the rule for the parent.

/* Any .menu that contains an item with class .active */
.menu:has(.active) {
  background: #f4f4f4;
}

/* Only a DIRECT child counts: use the > combinator */
.menu:has(> li.active) {
  outline: 2px solid currentColor;
}

/* A list item that is NOT holding an .active link */
li:not(:has(a.active)) {
  opacity: 0.7;
}

Without the >, :has(.active) matches at any depth, so an outer wrapper several levels up also matches. Add > when you only want the immediate parent.

Practical patterns

  • Quantity and content queries: style a gallery differently when it has exactly one image versus many - .gallery:has(img:only-child).
  • Previous-sibling styling: CSS could only ever look forward with + and ~; :has() lets an earlier element react to a later one, e.g. label:has(+ input:checked).
  • Layout that adapts to content: give a card a two-column layout only when it actually contains an image, and a single column when it does not.
  • Form and validation states: highlight a whole fieldset when any control inside is invalid, without a line of script.

Browser support

Support is broad in 2026. :has() is Baseline across current browsers:

BrowserFirst version with :has()Released
Safari (macOS and iOS)15.4March 2022
Chrome, Edge, Opera105August to September 2022
Firefox121December 2023

For older browsers, treat :has() styles as a progressive enhancement: a browser that does not understand the selector drops that rule and keeps the rest of the stylesheet. If you need a different fallback layout, test for support explicitly with @supports selector():

.card { display: block; }               /* fallback for every browser */

@supports selector(:has(img)) {
  .card:has(img) { display: grid; grid-template-columns: 200px 1fr; }
}

One detail that trips people up: never group a :has() selector with other selectors in the same rule (.a, .b:has(img) { }). In a browser without support the whole rule is invalid, so .a loses its styles too. Keep :has() rules separate.

Does :has() have a performance impact?

A normal selector is matched by looking at an element and its ancestors. :has() looks the other way, down into descendants or forward to siblings, so each time the DOM or an input state changes the engine may have to re-check ancestors that use it. Chromium, WebKit and Gecko all added invalidation optimisations so that only the affected subtrees are re-evaluated, which is why component-level use is cheap in practice. What still costs:

  • Unanchored, page-wide checks such as body:has(.x) or :has(.x) with nothing before it, on a page that changes often (live feeds, large tables).
  • Deep descendant arguments like .page:has(.a .b .c), which force a walk of the whole subtree.

Prefer a specific subject and the child combinator: .card:has(> img) is cheaper to evaluate than .card:has(img), and both are far cheaper than :has(img). To check a real page, record a trace in the browser's Performance panel while you interact and look at the "Recalculate Style" time before and after adding the rule.

Gotchas worth knowing

  • You cannot match pseudo-elements inside :has(). Selectors like ::before are not allowed as the argument.
  • :has() itself is not allowed inside :has(). You cannot nest one relational check inside another.
  • Keep the scope reasonable. Very broad selectors like body:has(...) that force the browser to re-check large parts of the page can cost performance on big, frequently-updating documents. In practice, scoped selectors on components are fine; engines have optimised the common cases.
  • The target is the subject, not the match. If a rule is not applying, check that you are styling the element before :has(), not the one inside it.

FAQ

Is :has() really the CSS parent selector? Effectively yes, and more. It selects an ancestor based on its descendants, which is the classic "parent selector" use case, but because it accepts combinators it can also select based on following siblings, so it is better described as a relational selector.

Can I use :has() in production in 2026? For most sites, yes - it is supported in the current versions of all major browsers. Use it as a progressive enhancement if you must support very old browsers.

How do I select a parent only if a child has a class? Write .parent:has(.child-class), or .parent:has(> .child-class) to limit the match to direct children.

Does :has() hurt performance? Rarely, if you keep selectors scoped. Extremely broad, document-wide :has() checks on large, constantly changing pages can add cost, but component-level usage is fine.

For more modern CSS, see our guides on native CSS nesting and container queries. Ready to put a CSS demo online? Our static website hosting guide compares the free and cheap options.