This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović
You already know that the :has()
pseudo-class allows you to check whether a parent element contains certain children, but you can also make this selector more specific, or check other relations the element might have.
Child combinators
You can check whether an element contains a specific direct child element.
For example, if you have a fieldset
and you want to make sure that it contains a legend
and that this legend
is actually a direct child item of the fieldset
, which is important, you could use the child combinator (>
) in your :has()
pseudo-class.
fieldset:not(:has(> legend)) {
border: 10px solid red;
}
<fieldset>
<div>
<legend>Letters</legend>
</div>
<input type="radio" name="letters" id="a">
<label for="a">a</label>
<input type="radio" name="letters" id="b">
<label for="b">b</label>
</div>
</fieldset>