:has(): The Parent Selector That Replaces JavaScript State Classes
Use the CSS :has() parent selector to style parents, siblings and page state from the DOM: forms, cards, empty states, scroll locks, and performance tips.
:has() matches an element based on what it contains or what follows it, so CSS can finally style a parent from its children’s state, a previous sibling from a later one, and the whole page from a single open dialog. It has worked in every major browser since December 2023. A large share of the JavaScript that only adds and removes state classes, such as .has-error, .is-checked or body.modal-open, can be deleted. The rule that keeps it maintainable: JavaScript writes semantic state into the DOM, and CSS reads it with :has().
Look at a typical form library and you will find code like this: listen for input events, check validity.valid, then call closest('.field').classList.toggle('has-error') so the label and border can turn red. The browser already knows the field is invalid. The script exists only because CSS could not look up the tree from the input to its wrapper.
The :has() pseudo-class, often called the parent selector, takes a list of relative selectors and matches an element if any of them match relative to it. .field:has(input:user-invalid) matches a field that contains an invalid input. Because the argument is relative, it can also look at following siblings, so it works as a previous-sibling selector too. It is defined in Selectors Level 4, and Firefox 121 completed cross-browser support in December 2023.
My position: “parent selector” undersells it. :has() is a way to make the DOM the single source of truth for UI state. Every state class you mirror in JavaScript is a second copy that can drift.
Syntax: four directions from one selector
The argument starts with an optional combinator. Without one, it means “descendant”.
| Selector | Matches a .card that… |
|---|---|
.card:has(img) |
contains an image anywhere inside |
.card:has(> img) |
has an image as a direct child |
.card:has(+ .card) |
is immediately followed by another card |
.card:has(~ .card--featured) |
has a featured card somewhere after it |
.card:not(:has(img)) |
contains no image at all |
.card:has(img, video) |
contains an image or a video |
.card:has(img):has(.price) |
contains both an image and a price |
The subject is still the element before :has(). In .card:has(img) h3, the styled element is the h3, inside a card that has an image. Reading right to left, as described in the selectors guide, still applies.
Eight patterns that replace JavaScript
1. Form field state
.field:has(input:user-invalid) label { color: var(--color-danger); }
.field:has(input:user-invalid) .error { display: block; }
.field:has(:focus-visible) { outline: 2px solid var(--focus-ring); outline-offset: 4px; }
.field:has(input:required) label::after { content: " *"; color: var(--color-danger); }
Using :user-invalid rather than :invalid means the error appears only after the user has interacted with the field. No event listeners, no class toggling, and the visual state always matches the browser’s validation state.
2. Layout by content
.card { display: grid; gap: 1rem; }
.card:has(> img) {
grid-template-columns: 10rem minmax(0, 1fr); /* image cards go side by side */
}
.card:not(:has(> img)) {
border-inline-start: 4px solid var(--color-accent); /* text-only cards get an accent */
}
Content editors no longer have to pick “card with image” or “card without image” in the CMS. The card adapts to what it contains.
3. Scroll lock while a modal is open
html:has(dialog:modal) {
overflow: hidden;
scrollbar-gutter: stable; /* avoid a layout jump when the scrollbar disappears */
}
This replaces the document.body.classList.add('modal-open') call that every modal library used to ship, and it cannot get stuck if the dialog closes through Escape or a form submission.
4. Highlight the selected table row
tr:has(input[type="checkbox"]:checked) {
background: var(--color-selected);
}
/* Show the bulk action bar only when something is selected */
.table-toolbar { display: none; }
.data-table:has(tbody input:checked) .table-toolbar { display: flex; }
5. Empty states
.results:not(:has(.result)) .results__empty { display: block; }
.results:has(.result) .results__empty { display: none; }
The empty message now appears whenever the list is empty, whether it was empty on load, emptied by a filter, or cleared by deleting the last item.
6. Quantity-aware layouts
/* Switch to a compact layout when the menu has more than six items */
.menu:has(> li:nth-child(7)) { font-size: var(--text-sm); gap: 0.25rem; }
7. Previous-sibling effects
/* Dim the cards around the one being hovered, including the ones before it */
.cards:has(.card:hover) .card:not(:hover) { opacity: 0.6; }
/* Tighten space above a heading that directly follows a figure */
figure:has(+ h2) { margin-block-end: 0.5rem; }
Before :has(), CSS could only select elements after a given one. Now it can select elements before it, which is what the second rule does: it styles the figure based on what comes next.
8. A theme switch with no script for the styling
html:has(#theme-dark:checked) { color-scheme: dark; }
html:has(#theme-light:checked) { color-scheme: light; }
Paired with the light-dark() tokens from the custom properties guide, a radio group switches the whole theme. You still need a few lines of JavaScript to remember the choice across visits, but the styling itself has no script.
JavaScript writes semantics, CSS reads them
:has() does not remove the need for JavaScript; it changes what JavaScript is for. Scripts still manage focus, fetch data, persist preferences and update ARIA attributes. What disappears is the layer of scripts whose only job was to copy state into class names.
/* JavaScript sets the accessible state... */
button.setAttribute("aria-expanded", String(open));
/* ...and CSS reads it, from any ancestor */
.nav:has([aria-expanded="true"]) .nav__backdrop { display: block; }
.site-header:has(.nav [aria-expanded="true"]) { box-shadow: var(--shadow-raised); }
If the script forgets to update aria-expanded, the menu also looks wrong, so the accessibility bug becomes a visible bug that gets reported and fixed. With a separate .is-open class, the two could silently disagree.
Specificity and syntax rules
- Specificity:
:has()takes the specificity of its most specific argument, like:is()..card:has(#promo)scores (1, 1, 0). Wrap it in:where()when you want a zero-weight default::where(.card:has(img)). - No nesting:
:has()cannot appear inside another:has(). - No pseudo-elements inside:
:has(::before)is invalid. - Unforgiving list: unlike
:is(), an invalid selector in the argument list makes the whole rule invalid.
Performance: keep the subject narrow
Every :has() rule creates work for the browser when the DOM changes. If a descendant is added, removed or changes state, the engine must check whether any ancestor’s :has() match changed. Engines optimize this heavily, but the cost still depends on how broad the rule is.
- Cheap:
.field:has(> input:user-invalid). Narrow subject, child combinator, rarely changing state. - Moderate:
.card:has(img). A descendant search, but bounded by the card. - Expensive on big pages:
body:has(.something)or:has(*:hover)on a large subject. Any mutation or hover anywhere can trigger re-checks across the document.
Anchor :has() to the smallest element that makes sense, prefer the child combinator when you can, and use page-level rules like the modal scroll lock only for states that change rarely. If the Performance panel shows long “Recalculate Style” tasks after interactions, page-wide :has() rules are among the first things to check. The performance-safe CSS guide covers how to measure it.
Feature detection
Every current browser supports :has(). If you must support older browsers, such as Firefox ESR releases from before version 121, detect it with a feature query and keep the JavaScript class as a fallback only there:
@supports selector(:has(a)) {
.field:has(input:user-invalid) label { color: var(--color-danger); }
}
@supports not selector(:has(a)) {
.field.has-error label { color: var(--color-danger); }
}
Frequently Asked Questions
Is :has() a parent selector?
Yes, and more. :has() matches an element if the relative selector inside it matches, so .card:has(img) selects cards that contain an image. Because the argument can start with + or ~, it can also select an element based on its following siblings, which makes it a previous-sibling selector too.
Is CSS :has() supported in all browsers?
Yes. :has() is supported in Chrome and Edge since version 105, Safari since 15.4, and Firefox since 121, which completed cross-browser support in December 2023.
Does :has() hurt performance?
Usually not. Browsers optimize it heavily. The cost grows when the subject is very broad, such as body or html, and the state inside changes often, because every change can trigger style re-checks across the page. Anchor :has() to small components and prefer the child combinator.
Can :has() replace JavaScript completely?
No. It replaces scripts that only copy state into class names. JavaScript is still needed for focus management, saving preferences, fetching data and updating ARIA attributes, which :has() can then read.
Let the DOM be the source of truth
For years, CSS could only look down and forward, so JavaScript filled the gap by copying state into classes. :has() lets CSS look up and back. Delete the mirror classes, style native and ARIA state directly, and keep your :has() subjects small. Next, cascade layers tackle the other source of fragile CSS: specificity wars.
Rule of thumb: if a script’s only job is toggling a class to match state the DOM already holds, replace it with :has().
Originally posted on 28 August 2025 and post last modified on 28 August 2025