Selector List: SelectorA, SelectorB
When multiple selectors share the same declarations, they can be grouped into a comma-separated list.
It uses the selectorA, selectorB syntax, where a comma (,) separates the selectors and selectorA and selectorB share the same declarations.
This syntax is commonly referred to as multiple selectors or group selectors, while the specification officially calls it a selector list.
Syntax
selectorA, selectorB[, selectorC[, selector...] {
/* Style declarations shared by multiple selectors */
}
You can group multiple selectors into a list by separating them with commas. This allows all specified selectors to share the same style declarations.
The following two sets of style declarations are equivalent:
h1 {
font-family: serif;
color: red;
}
h2 {
font-family: serif;
color: red;
}
h3 {
font-family: serif;
color: red;
}
/* Equivalent to the following grouped declaration */
h1, h2, h3 { /* Share the same styles across the h1, h2, and h3 selectors */
font-family: serif;
color: red;
}
Advantages
As shown in the code example above, grouping multiple selectors into a comma-separated list makes the code more concise, readable, and easier to maintain when applying identical styles. This approach offers several key benefits, such as maintaining a consistent design across multiple selectors and simplifying maintenance by allowing style changes to be made in a single place. In addition, reducing repetitive style definitions can decrease the size of CSS files, which can positively impact web page loading speed.
Disadvantages
Although multiple selectors allow the same styles to be applied across multiple elements, one disadvantage is that if even one selector in the list is invalid (for example, because it is written incorrectly), the entire style rule is ignored.
For example, the following selector is incorrectly written and is therefore invalid:
...class {
font-family: serif;
color: red;
}
/* There is no class selector that begins with ...
It is incorrectly written; it should be .class. */
A class name placed immediately after . selects all elements that have that class. However, in the example above, .class was incorrectly written as ...class. CSS has no valid selector of this form.
Now, let's include the selector from the example above in a multiple-selector list:
h1, ...class , h2, h3 {
font-family: serif;
color: red;
}
/* Because the selector (...class) in the multiple-selector list is invalid,
the entire style rule, including the declarations for h1, h2, and h3, is ignored. */
As this example shows, if even one selector in a selector list is invalid, the entire style rule is ignored. Therefore, care must be taken when using multiple selectors.
Syntax Notes
A comma-separated list can be written on a single line or across multiple lines:
/* On a single line */
h1, h2, h3 {
font-family: serif;
color: red;
}
/* Across multiple lines */
h1,
h2,
h3 {
font-family: serif;
color: red;
}
When writing the list on a single line, whitespace before and after the commas separating the selectors is optional. However, adding whitespace is recommended to make the selectors easier to distinguish visually:
/* When writing the list on a single line,
whitespace before and after the commas is optional,
but adding whitespace is recommended for readability. */
h1,h2,h3 { /* Not recommended */
font-family: serif;
color: red;
}
h1, h2, h3 { /* Recommended */
font-family: serif;
color: red;
}
Be careful not to place a comma after the last selector in the list, as this makes the entire selector list invalid:
/* On a single line */
h1, h2, h3, {
font-family: serif;
color: red;
}
/* Across multiple lines */
h1,
h2,
h3, {
font-family: serif;
color: red;
}
/* A comma after the last selector (h3) makes the entire
selector list invalid, causing the entire style rule
to be ignored, including the h1, h2, and h3 selectors. */
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
SelectorA, SelectorB
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
SelectorA, SelectorB
|
Selectors Level 4 #grouping |
References
See Also
- CSS * Selector – Selects All Elements
- CSS Element Selector – Select by Element Name
- CSS Next-Sibling Combinator (+) – Select the Next Sibling Element
- CSS Descendant Combinator – Select All Descendant Elements
- CSS Child Combinator (>) – Select All Direct Child Elements
- CSS Subsequent-Sibling Combinator (~) – Select All Subsequent Sibling Elements