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.
: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.
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.
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.
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.
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.
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);
}.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.
.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.
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.
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.
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."
:nth-child() trick above, which breaks if the list reorders.:is() or :where() works, but each layer makes the selector harder to debug.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.
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);
}
}:has() selector as narrowly as the layout allows.:not(:has(...)) against :has(:not(...)) wherever a selector needs to express an absence.:has() as a stand-in for logic that needs real reactivity.@supports selector(:has(a)) fallback where legacy support genuinely matters.:has() selectors in code review.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.
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.
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.
: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.
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.
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.
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.