Group CSS Selectors with :is() and :where()
Grouping selectors without repetition
Repeating .card h1, .card h2, .card h3 is verbose. :is(h1, h2, h3, h4) under .card does the same in one rule.
The old hack vs the modern fix
View demo.card h1, .card h2, .card h3, .card h4 {
margin-bottom: 0.5em;
}
/* Same prefix repeated for every selector */.card :is(h1, h2, h3, h4) {
margin-bottom: 0.5em;
}Is browser support
Live demo
Why the modern way wins
No repetition
Write the shared part once. Add or remove items in the list without duplicating the prefix.
Easier to read
Intent is clear: these headings inside .card get the same style.
Takes specificity of argument
:is() uses the highest specificity in its list. Good to know when overriding.
- Lines Saved
- 6 → 3
- 50% less code
- Old Approach
- Repeated selectors
- .card h1, .card h2, ...
- Modern Approach
- :is() group
- One prefix, list of targets
How it works
The old way was to list every combination: .card h1, .card h2, .card h3, .card h4. Same prefix over and over. Adding h5 meant another comma and another .card h5.
.card :is(h1, h2, h3, h4) means .card plus any one of those. One prefix, one list. Change the list without touching the rest. :is() also keeps specificity predictable (the most specific selector in the list wins).