← All posts

The CSS :has() selector: 5 practical cases that replace JavaScript

:has() finally lets you style an element based on what it contains. These are the uses that save us time on client sites, plus the limits worth knowing.

The CSS :has() selector: 5 practical cases that replace JavaScript

Key points

  • :has() targets a parent or a preceding element based on what it contains.
  • A lot of small UI scripts can become a single CSS rule.
  • Every modern browser supports it; make sure the page still works without it.

For years the answer to “how do I style a parent element?” was “you can’t in CSS, add a class with JavaScript”. That gave us small scripts adding .has-image or .is-invalid, which forgot to re-run when the content changed. :has() makes much of that code unnecessary.

How it works

A:has(B) matches A when it contains something matching B. It also works with sibling combinators: h2:has(+ p) matches a heading immediately followed by a paragraph.

Five real-world uses

1. Cards with or without an image. In a WordPress archive, some posts have no featured image:

.card:has(img) { display: grid; grid-template-columns: 160px 1fr; }
.card:not(:has(img)) { border-left: 4px solid var(--accent); }

2. A form field in error. Colour the whole field, label included, but only once the user has typed something:

.field:has(input:invalid:not(:placeholder-shown)) label { color: #b42318; }

3. Lock page scrolling while a dialog is open, no extra script:

body:has(dialog[open]) { overflow: hidden; }

4. Adjust a grid to its item count:

.grid:has(> :nth-child(4)) { grid-template-columns: repeat(2, 1fr); }

5. Highlight the selected option on a pricing form: .plan:has(input:checked) { outline: 2px solid var(--accent); }

Limits and caveats

  • Progressive enhancement: modern browsers all support it, but an old one drops the whole rule. The base layout must still be usable.
  • Performance: a very broad selector such as body:has(.x) makes the browser re-check a lot of elements on every DOM change. On pages that update often, scope it to a nearby container.
  • No nesting: :has() cannot be used inside another :has().

Our rule of thumb

On our projects, JavaScript keeps the logic (submitting, server-side validation, dynamic accessibility), and :has() takes whatever is purely visual. The code gets shorter, and the styling can no longer drift out of sync with what is actually on the page.

A typical case: on quote request forms, error colours, the selected option and the scroll lock are often handled by three separate scripts. Moving them to :has() rules also gets rid of the classic bug where a field keeps its error state after the user has fixed it. One less thing to test after every plugin update.

FAQ

Can I detect support?

Yes: @supports selector(:has(a)) { … } applies a layout only in browsers that understand it.

Does it work in the WordPress block editor?

Yes. The editor runs in a modern browser, so your :has() rules apply in the preview as long as your stylesheet is loaded there.

Looking for a lighter interface with fewer scripts to maintain? Our design and front-end team can help.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →