Web development · 30 Aug 2014 · 3 min read
How to apply the same CSS style to multiple IDs
You have three elements, #header-search, #footer-search and #sidebar-search, and you want them all to look the same. You don’t need three copies of the same rule.
The short answer: a selector list
Separate the IDs with commas. The rule applies to every element in the list:
#header-search,
#footer-search,
#sidebar-search {
width: 100%;
padding: 0.5rem 0.75rem;
border: 1px solid #ccc;
}
This works in every browser, old and new. One thing to know: if any selector in a plain list is invalid (a typo, or syntax an older browser doesn’t understand), the browser drops the whole rule, not just that one selector.
Shorter with :is()
:is() takes a list of selectors and matches any of them. It is handy when the IDs share a context:
main :is(#header-search, #footer-search, #sidebar-search) input {
font-size: 1rem;
}
Without :is() you would have to write main #header-search input three times. Two details:
- Specificity.
:is()takes the specificity of its most specific argument. With IDs inside, the whole thing counts as an ID selector, which is hard to override later. - Forgiving. An invalid selector inside
:is()is ignored on its own, so it doesn’t break the rest of the rule.
Zero specificity with :where()
:where() matches exactly like :is(), but always has zero specificity. Use it for base styles you expect to override:
:where(#header-search, #footer-search, #sidebar-search) {
border-radius: 4px;
}
/* A plain class can now override it */
.search-compact {
border-radius: 0;
}
Matching IDs by pattern
If the IDs follow a naming pattern, an attribute selector can catch all of them without listing each one:
/* Every element whose id starts with "search-" */
[id^="search-"] {
width: 100%;
}
/* Every element whose id ends with "-search" */
[id$="-search"] {
width: 100%;
}
^= means “starts with”, $= “ends with” and *= “contains”. Attribute selectors have class-level specificity, lower than an ID.
Usually better: give them a class
If you find yourself listing IDs to share a style, the elements probably belong to the same component. That is what classes are for:
<input id="header-search" class="search-field" type="search">
<input id="footer-search" class="search-field" type="search">
.search-field {
width: 100%;
padding: 0.5rem 0.75rem;
}
The IDs stay for what they are good at (a label’s for, an anchor link, JavaScript), and the styling lives on one class that any new search box can reuse. An ID must be unique on the page, and an element can only have one, so a class is also the only way to share a style between repeated elements.
The same selectors work in JavaScript
document.querySelectorAll() accepts the same comma-separated list:
document
.querySelectorAll("#header-search, #footer-search, #sidebar-search")
.forEach((el) => el.classList.add("is-ready"));
For another small JavaScript task, see how to redirect to a new page after a few seconds.
Quick reference
| You want | Use |
|---|---|
| A few known IDs, one rule | #a, #b, #c { … } |
| The IDs inside a shared context | main :is(#a, #b) input { … } |
| Base styles that are easy to override | :where(#a, #b) { … } |
| IDs that follow a naming pattern | [id^="prefix-"] { … } |
| Elements that are really the same component | A shared class |