DEV Community

Cover image for CSS Selectors: Unlocking Advanced Selectors for Modern Web Design

CSS Selectors: Unlocking Advanced Selectors for Modern Web Design

Eleftheria Batsou on November 21, 2024

Hey everyone! Welcome to my blog. 👋 Introduction Today, we're diving into the world of advanced CSS selectors. These selectors, like :i...
Collapse
 
leob profile image
leob

I knew about :not, but :has is interesting, didn't know about that one!

I'm a bit puzzled by :is and :where, because to me it seems that simple standard selectors (without :is or :where) work almost the same ... probably some subtlety that I'm missing? ;-)

Collapse
 
maxart2501 profile image
Massimo Artizzu

They're basically the same, functionally. The only difference is specificity, as Eleftheria said: the selectors that you put into :where() do not contribute to the specificity of the selector, whereas what you put into :is() does.

Collapse
 
leob profile image
leob

Thanks for the clarification!

Collapse
 
madza profile image
Madza

Awesome article as always, Eleftheria! 💯

Collapse
 
eleftheriabatsou profile image
Eleftheria Batsou

Thank you!

Collapse
 
joodi profile image
Joodi

Thank you for sharing such an amazing and practical article! The explanations and examples are super clear and helpful. I’ve shared this post on my LinkedIn so others can benefit from it too. 🙌🌟

linkedin.com/feed/update/urn:li:ac...

Collapse
 
eleftheriabatsou profile image
Eleftheria Batsou

Thank you so much for sharing it! 😊

Collapse
 
mudskipper_dev profile image
SeanImz

In this example:
:is(.alert.success, .alert.error, .alert.warning) {
font-weight: bold;
border: 1px solid;
border-radius: 4px;
}

Wouldn't this as written work the same without the :is() ?
Should it have read :is(alert) without the other classes?

Also the reference to :has being limited to recent versions of Safari seems well out of date as of the posting date, as it's in all the major browsers now?

Collapse
 
mariohernandez profile image
Mario Hernandez

I agree. In fact, you can write the same rule by simply using the .alert class since all of them have it. I also agree about :has. All major browsers now have full support for it.

Collapse
 
eleftheriabatsou profile image
Eleftheria Batsou

Thank you for this note!

Collapse
 
omidheidarzadeh profile image
Omid Heidarzadeh

In fact this snippet can be refactored like this:

.alert:is(.success, .error, .warning) {
font-weight: bold;
border: 1px solid;
border-radius: 4px;
}

Collapse
 
eshimischi profile image
eshimischi

You can combine :is and :not.

Collapse
 
pozda profile image
Ivan Pozderac

You can also combine :not with older selectors like nth-child, last-of-type etc

Collapse
 
eshimischi profile image
eshimischi

Within the context of this article nothing mentioned about joint use of each selector

Collapse
 
eleftheriabatsou profile image
Eleftheria Batsou

🤓👌

Collapse
 
yosvaldo profile image
Yeshua Osvaldo

Amazing! Thanks for sharing :)

Collapse
 
eleftheriabatsou profile image
Eleftheria Batsou

My pleasure! Thanks for reading.