:is() and :where() for Grouping Selectors
You'll learn how to write cleaner, more maintainable CSS by grouping complex selectors without repeating yourself.
The Problem with Traditional Grouping
Imagine you want to style links inside several different containers. Traditionally, you'd write:
header a:hover,
nav a:hover,
footer a:hover {
color: blue;
}
This works, but it gets repetitive fast. The bigger your selector list, the harder it is to maintain.
What :is() Does
The :is() pseudo-class lets you group selectors inside your rule, eliminating duplication:
:is(header, nav, footer) a:hover {
color: blue;
}
Think of :is() as saying "if the element matches any of these selectors." It's like a parenthetical "or" statement for CSS.
What :where() Does
:where() works exactly the same way as :is(), but with one crucial difference: it has zero specificity. This makes it perfect when you want maximum flexibility for overrides:
:where(header, nav, footer) a:hover {
color: blue;
}
/* Easy to override because :where() adds no specificity weight */
nav a:hover {
color: red;
}
Key Differences
- :is() takes the specificity of its most specific selector
- :where() always has zero specificity
- Both work identically for grouping
When to Use Each
Use :is() when you want normal specificity behavior. Use :where() when building base styles or component libraries where easy overriding is important.
Key Takeaway: :is() and :where() reduce selector duplication by letting you group alternatives inside a selector—:is() maintains specificity while :where() eliminates it entirely.