Checkout my Getting Started with React video course on Skillshare. It is 100% free.

What is the :is() CSS pseudo-selector and how it is helpful

The :is() gets a list of arguments and applies the given rules to the all elements that are matching on of the selectors in the list. For example:

p:is(.main, .front) {
    font-weight: bold;
}
/* will make bold any p element that 
has at least a class of main or front */

This is a less verbose way of writing a list of CSS selectors.

p.main, 
p.front {
    font-weight: bold;
}

We can pass it anything, not just classes. We can use it in conjunction with ids, classes, tags etc.

:is(h1, h2) {....}
.login:is(div, #main, .invaid) {...}

The specificity will equal that of the passed selector with the highest specificity. So, :is(#main, div) will have the specificity of the #main - 1.0.0 and :is(h1, form) will have a specificity of 0.0.1.

Also one cool thing about the :is() selector is that it will work even if one argument is not valid. As the opposite of the classical way of writing CSS when having one invalid selector will cancel the full list.

p, 777 {...};
/* will not work not even for the p elements*/
:is (p, 777) {....}
/* will not work for the p elements*/

This makes it great to be used within selectors that are not yet fully supported in all browsers.

:is(p, webkit-unsuported-suff-here) {...}
/*will still work for the p elements*/ 

The cross-browser support is pretty good.

Home Screencasts Best of Newsletter Search X