By utilizing CSS pseudo-classes :is (previously known as :any and :matches) along with :where, you can leverage the use of commas to match multiple classes on various levels.
When used at the root level, :is(.abc, .xyz)
and .abc, .xyz
exhibit similar functionality. However, :is enables matching only a portion of the selector without repeating the entire selector multiple times.
For instance, if you wish to target both .root .abc .child
and .root .xyz .child
, you can implement code like this:
.root :is(.abc, .xyz) .child {
margin-left: 20px;
}
<div class="root">
<a class="abc">
<span class="child">Lorem</span>
</a>
<a class="xyz">
<span class="child">Ipsum</span>
</a>
</div>
The distinction between :is and :where lies in the fact that :where is disregarded when determining the specificity of the selector:
- specificity of
.root :is(.abc, .xyz) .child
= specificity of .root .abc .child
- specificity of
.root :where(.abc, .xyz) .child
= specificity of .root .child
Both pseudo-classes recognize a forgiving selector list, meaning that if one selector fails to parse correctly (due to unsupported syntax, being too new, too outdated, or simply incorrect), the other selectors will still function. This attribute makes :is valuable even at the root level since it allows for the combination of selectors using modern CSS features without apprehension about one selector causing issues with the rest.
P.S. While addressing a more challenging aspect of the question, Google tends to bring up this page for numerous queries, hence why I believe this information may be pertinent here.