Elementary Standards
HTMLCSSWeb StandardsSEOBlogStart reading
Home / Blog / The CSS :has() Selector: A Practical Guide
CSS

The CSS :has() Selector: A Practical Guide

The selector CSS lacked for two decades — how to style a parent by what it contains, and where it still falls short.

7 min read·2026-11-27·James Webb

For as long as CSS has existed, it has had no way to select an element based on what's inside it. You could style a child based on its parent, but never the reverse — if a form field was invalid, you couldn't style the wrapper around it without JavaScript reaching in and toggling a class. :has() finally closes that gap. TL;DR: :has() is CSS's parent selector — it matches an element when a given relative selector matches something inside it or after it. It works in every current major browser in 2026, and this guide covers the four relational patterns, the real-world patterns worth adopting, the one gotcha that trips people up, and where it still can't help.

The pain this solves is familiar to anyone who has shipped a form: you want the field's wrapper to go red when the input inside it is invalid, so instead you write a script that listens for validation events and toggles a class by hand — a job CSS should have been able to do on its own. Teams that still reach for that kind of JavaScript patch for container-level state are often the ones carrying technical debt that resurfaces the moment they move toward standards-compliant web development at YuSMP and similarly rigorous builds, where declarative CSS is expected to do declarative CSS's job.

Here's what :has() actually does, the patterns worth stealing, and the two gotchas that trip people up.

What :has() Actually Does

:has() is a relational pseudo-class: it matches an element when a relative selector passed to it matches something relative to that element — a descendant, a child, or a sibling. Unlike every other CSS selector, it lets you select based on content rather than the element's own attributes or state.

/* Style a card that contains an image, and only that kind of card */
card:has(img) {
  grid-template-columns: 120px 1fr;
}

Nothing inside the parentheses is itself styled. The parentheses describe a condition; the selector outside them is what actually gets the rule.

The Four Relational Patterns

Any descendant — :has(...)

article:has(blockquote) {
  border-left: 3px solid var(--accent);
}

Reach for this when the thing you're checking for could be nested at any depth inside the element — a quote buried three levels into an article body, for example.

Direct child — :has(> ...)

ul:has(> li.featured) {
  background: var(--highlight);
}

Use the child combinator when you need to rule out a false match from a deeply nested element that happens to share the same class or tag name.

Immediately following sibling — :has(+ ...)

h2:has(+ p.lead) {
  margin-bottom: 4px;
}

This is for the one case CSS genuinely never had before: styling something based on what comes directly after it, not before.

Any following sibling — :has(~ ...)

fieldset:has(~ .form-error) {
  border-color: var(--danger);
}

Reach for the general sibling combinator when the triggering element could appear anywhere later in the same parent, not necessarily right next door.

Patterns Worth Stealing

Style the wrapper, not just the input

This is the highest-value use case: validation state that lives on the wrapper, driven entirely by the input's own state.

.field:has(input:invalid) {
  border-color: var(--danger);
}
.field:has(input:focus) {
  outline: 2px solid var(--accent);
}

Cards that adapt to their own content

.card:has(img) {
  grid-template-columns: 96px 1fr;
}
.card:not(:has(img)) {
  grid-template-columns: 1fr;
}

One component, two layouts, zero conditional classes added by a template or a framework.

Styling the previous sibling

.step:has(~ .step:hover) {
  opacity: 0.5;
}

Before :has(), CSS could style the next sibling on hover but never the one before it — there was no "previous sibling" combinator at all. This pattern fakes one.

Quantity queries

ul:has(> li:nth-child(6)) {
  max-height: 240px;
  overflow-y: auto;
}

Read this as "once this list has at least six items." The sixth child only exists once the list is long enough, so the selector only matches past that threshold.

Page-level state without JavaScript

body:has(#drawer-toggle:checked) {
  overflow: hidden;
}
body:has(#drawer-toggle:checked) .drawer {
  transform: translateX(0);
}

Combined with the checkbox hack, :has() lets a single hidden checkbox drive page-level state — a locked scroll, a visible drawer — with no script at all.

Why Does :not(:has(...)) Behave Differently From :has(:not(...))?

These two look like they should be interchangeable negations of the same thing. They aren't, and the difference matters the first time you need "a card with no image" versus "a card where something isn't an image."

.card:not(:has(img)) matches a card that contains zero images anywhere inside it — the negation applies to the whole "has an image" condition. .card:has(:not(img)) matches a card that contains at least one element that isn't an image — almost any card with more than one child, since a caption or heading satisfies "not an image." If a card has both a heading and an image, :has(:not(img)) still matches, because the heading alone is enough.

<div class="card">
  <h3>Caption</h3>
  <img src="photo.jpg" alt="">
</div>
/* .card:not(:has(img))  → does NOT match (the img is present) */
/* .card:has(:not(img))  → MATCHES (the h3 satisfies "not an img") */

When what you mean is "doesn't contain X," reach for :not(:has(X)). Save :has(:not(X)) for the much rarer case where you genuinely want "contains something other than X."

What :has() Still Can't Do

Specificity and Performance

Specificity follows the normal rule: :has() takes on the specificity of the most specific selector inside its parentheses, not a flat value of its own. A :has() containing an ID selector is as specific as that ID.

Performance is less scary than it sounds. Current engines use Bloom filters and ancestor-invalidation tracking that keep :has() close to an ordinary descendant selector. The actual risk is scope, not the pseudo-class itself: a broad, unscoped :has() near the root of a large document forces more of the tree to be re-checked on every mutation. Scope it as tightly as you can.

Browser Support and Progressive Enhancement

Every current major browser engine — Chromium, Firefox, and Safari — supports :has(), and it's been safe to ship in production for some time. For very old browser versions, feature-detect with @supports before relying on it for anything load-bearing:

@supports selector(:has(a)) {
  .nav:has(a:focus-visible) {
    outline: 2px solid var(--accent);
  }
}

A Pre-Production Checklist for :has()

Frequently Asked Questions

Is :has() supported in all browsers in 2026?

Yes — every current major browser engine (Chromium, Firefox, Safari) has shipped :has() support, and it has been safe to use unconditionally in production for some time. For anyone still supporting very old browser versions, wrap usage in an @supports selector(:has(a)) check.

Does :has() replace the need for JavaScript entirely?

No. It replaces JavaScript for a specific class of problem: toggling a parent's appearance based on something true about its descendants or siblings. It is declarative CSS, not an event system, so it has no equivalent for logic that depends on user actions, timers, or external data.

Can I nest :has() inside :has()?

Yes, the selector list inside :has() can itself contain other pseudo-classes including :has(), :is(), and :not(). Nesting works, but each additional layer makes the selector harder to read and reason about, so keep nested :has() chains rare and well commented.

What's the difference between :has() and :is()?

:is() matches the element itself against a list of selectors — it's a shorthand for writing several selectors at once. :has() matches an element based on what's inside or after it. :is() never changes what element is selected; :has() can select a completely different element than what's inside its parentheses.

Does :has() hurt CSS performance?

Modern engines use Bloom filters and ancestor invalidation, keeping :has() close to an ordinary descendant selector in most cases. The risk is writing a broad, unscoped selector that forces the browser to re-check large parts of the DOM on every mutation — not the pseudo-class itself.

Can :has() select based on sibling count?

Indirectly, by combining :has() with :nth-child(). A selector like ul:has(> li:nth-child(6)) matches a list once it has at least six items, because the sixth child only exists at that point. There's no direct count() syntax, but this combination covers most quantity-query needs.

Is :has() part of an official spec, or still experimental?

It's part of the CSS Selectors Level 4 specification from the W3C, not an experimental or vendor-prefixed feature. Combined with full support across current major browser engines, it's safe to treat as a standard, stable part of the CSS language. The MDN reference for :has() is the fastest way to check edge cases as they come up.

:has() doesn't make CSS reactive in the JavaScript sense, and it isn't trying to. What it does is close a twenty-year gap: letting a parent respond to its own content without a script watching for it. Learn the four relational patterns, keep selectors scoped, and reach for :not(:has()) instead of :has(:not()) whenever "doesn't contain" is what you actually mean.

Back to the blog
More notes

Keep reading