:has()
A CSS pseudo-class that matches an element if something inside it, or after it, matches the given selector, such as .card:has(img).
In more detail
It works like the long-requested parent selector: style a form field that has an invalid input or a card that contains an image without adding classes from JavaScript. Its specificity is that of its most specific argument.
Example
.field:has(input:checked) { border-color: green; }Related terms
- pseudo-class — A selector keyword starting with a colon, such as :hover, :checked or :has(), that matches elements by state or relationship rather than by name.
- :is() — A CSS pseudo-class that matches any element matching one of the selectors in its list, such as :is(header, form) input.
- form state pseudo-classes — Pseudo-classes such as :checked, :invalid and :disabled that match form controls by their current state.
Taught in
- Modern Selectors: :has(), :is(), :where() And Nesting Standards — Frontend Architecture & Web Platform