šŸŽ Checkout my Learn React by Making a Game course and get 1 month Free on Skillshare!

What is the :is() CSS pseudo-class 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.

šŸ“– 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.

šŸ“– 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.


Leave a Reply

Your email address will not be published. Required fields are marked *