How to use :has() in css

The :has() CSS pseudo-class represents an element if any of the selectors passed as parameters match at least one element.

But, it’s more than a “parent” selector. That’s a nice way to market it. The not-so-appealing way might be the “conditional environment” selector. But that doesn’t have quite the same ring to it. How about the “family” selector?

the following selector matches only <a> elements that contain an <img> child:

a:has(> img)

The following selector matches a <dt> element immediately followed by another <dt> element:

dt:has(+ dt)

The following selector matches <section> elements that don’t contain any heading elements:

section:not(:has(h1, h2, h3, h4, h5, h6))

Note that ordering matters in the above selector. Swapping the nesting of the two pseudo-classes, like:

section:has(:not(h1, h2, h3, h4, h5, h6))