`:has()` for layout and state decisions
Style the current element only when an internal condition is true.
For years CSS could not select a parent according to what happened inside it. `:has()` changes that and opens patterns that previously required JavaScript.
The right mental model is `element:has(condition)`. If the condition is true, the main element gets styled.
In `.card:has(img)`, you are not selecting the image. You are selecting the card because of its content. In forms, `form:has(:invalid)` makes global feedback much easier.
- Syntax: `element:has(selector)`.
- Typical cases: cards with or without media, menus with submenus, forms with validation errors.
- You can combine `:has()` with `:not()`: `article:not(:has(img))`.
- Keep the scope component-based: `.checkout-form:has(:invalid)` is usually better than a broad global rule.
- Support is strong in modern browsers, but still worth checking for production requirements.