CSS :has Selector
Complete CSS Selector Reference Manual
Example
We want to apply styles to elements that contain .parent :has ( div ) { border: 1 px solid red ; } Try it » The CSS :has pseudo-class is a selector that allows you to select an element based on its descendant elements. The :has pseudo-class is very useful in CSS selectors because it can select elements based on more complex conditions. The numbers in the table indicate the first browser version that supports this property. If you want to select the parent element that contains a child element with the class name .active: div :has ( .active ) { background-color: yellow ; } Try it » If you want to select a parent element that contains both ul :has ( li ) :has ( div ) { border: 2 px dashed blue ; } Try it » The above code will add a blue dashed border to elements that contain both .parent elements:
Definition and usage
:has This pseudo-class is a relatively new feature in CSS and may not be supported by all browsers. Before using it, it is recommended to check browser compatibility.:has The pseudo-class cannot be used with * selectors; it can only be used with type selectors (such as div, span etc.) or class selectors.
Browser support
Selector
:invalid
10.0
10.0
4.0
5.0
10.0
CSS Syntax
parent:has(selector) {
/* Style rules */
}
parent: the selector representing the parent element.selector: the selector representing the child element you want to find within the parent element.
More examples
Select elements containing a specific class name
Example
Select elements containing multiple specific child elements
Example
and element:
and