Get Ready for the Fabulous, Mysterious, and Quirky :has Pseudo-class in Frontend Development!
The CSS :has() selector is the 'parent selector' we've wanted for years — here's why it matters.
The selector that looks up the tree
For decades CSS let you style children based on parents, but never the reverse. `:has()` changes that: it lets you select an element based on what's inside it. A card that contains an image, a form that contains an invalid input, a nav item that contains an open menu — all now selectable in pure CSS.
This removes entire categories of JavaScript that existed only to add a class when some descendant matched a condition.
The patterns that change
The most exciting wins are state-driven styling: `form:has(input:invalid)` to highlight a form with errors, or `li:has(ul)` to mark a nav item as a parent. These are real UI states expressed declaratively, in the language that already owns the visuals.
Combine it with `:not()` and sibling selectors and you get expressive, self-documenting styles that stay in sync with the DOM automatically.
Use it with care
As with any powerful selector, watch the specificity and performance on huge documents. But for the vast majority of UI work, `:has()` is a gift — the selector we spent years pretending we didn't need.