4

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

 3 years ago
source link: http://www.js-craft.io/blog/what-is-the-is-css-pseudo-selector-and-how-it-is-helpful/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.

I hope you have enjoyed this article and if you would like to get more articles about React and frontend development you can always sign up for my email list.

Newsletter subscribe:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK