CSS
Real-World :has() Selector Patterns
Real-World :has() Selector Patterns
The :has() selector enables parent-aware styling in CSS, reducing JavaScript for many interface states.
High-value use cases
- Form groups that react to invalid inputs
- Cards that change style when they contain highlighted content
- Navigation items reacting to active children
Why teams like it
You can express UI state relationships directly in CSS. This keeps behavior predictable and styling logic centralized.
Form pattern
Mark a field wrapper when any child input is invalid and show a stronger border and hint color.
Content pattern
If a card contains a warning badge, change card accent style automatically.
Adoption note
Use progressive enhancement if you support older browsers. For modern products, :has() significantly simplifies style rules.

Comments
0Sign in to leave a comment.