Why :is is helpful
The :is()
pseudo-class in CSS may seem useless at first, because you generally can get the same result without using it. Make no mistake, however, because thanks to this pseudo-class you can sometimes shorten very long selectors and thus make your CSS file less complex and more readable.
:is()
Let's start with a (very) brief explanation of this pseudo-class. Like other pseudo-classes, this is a simple selector that makes it possible to make a selection based on information that is difficult to express with the typical use of selectors. In this particular case, we select elements that exactly match the selector list. Below is an example where both selectors have the same result.
Okay, but now the CSS just got longer!? Correct, but of course this example was for illustrative purposes only and not difficult to interpret at all. Now take the following example, where the style applies to all h1
elements that are nested in section
, article
, aside
or nav
:
Using :is()[/ c] we can set this up more easily, because just like a typical selector list we can also use nested elements here.
As you can see, this is a lot easier to interpret. It's about all the h1
elements that are under a section
/article
//aside
/nav
which in turn are also under a section
/article
//aside
/nav
.
On MDM a nice example is shown where a style is applied on all lists that are at least three levels deep.