How to Apply One CSS Rule to Several Selectors
To give several kinds of element the same styles, list their selectors in one rule, separated by commas. h1, h2 { ... } styles every h1 and every h2 on the page.
Syntax
<selector>, <selector> {
<declarations>
}
| Part | What it does |
|---|---|
<selector> | Any selector, such as h1 or .note. List as many as you need. |
, | Separates the selectors. The rule applies to every element that matches any of them. |
<declarations> | The styles every matched element gets. |
MDN's Selector list page calls this a comma-separated list of selectors.
The Example
The example is the start of a team handbook: a heading, a subheading and a paragraph. One rule colors both headings purple, and the paragraph keeps its black.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Selector list</title>
<style>
h1, h2 {
color: #5b5fc7;
}
</style>
</head>
<body>
<h1>Team handbook</h1>
<h2>Holidays</h2>
<p>Ask your manager before you book.</p>
</body>
</html>
The rule's selector is h1, h2, so every h1 and every h2 gets the color #5b5fc7.
Step 2: Open It in a Browser
Double-click index.html. It opens in your browser:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- Both headings are purple. Chromium gave the
h1and theh2the colorrgb(91, 95, 199), which is#5b5fc7. - The paragraph stayed black, because
pisn't in the list.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out the comma: h1 h2 | Without the comma, the space means "an h2 inside an h1", so neither heading changed | Separate the selectors with commas |
Leaving a comma at the end: h1, h2, | The list is invalid, so the whole rule was ignored and both headings stayed black | Remove the last comma |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| None of the selectors in the list work | One selector in the list is invalid, and that drops the whole rule: with h1, h2, p:hovering, nothing changed color | Fix or remove the invalid selector, or give it a rule of its own |
| Only some of the elements change | The others aren't in the list, or their selector is misspelled | Add or correct their selector |
MDN's Selector list page explains the first row: when a selector list contains an invalid selector, the entire style block is ignored.
FAQ
Can each selector go on its own line?
Yes. Line breaks around the commas don't matter, so a rule can start with button, on one line and input { on the next, and it still styles both.
Is a selector list the same as writing the rule twice?
Almost. h1, h2 { ... } styles the same elements as an h1 rule and an h2 rule with the same declarations. The difference is an invalid selector: in a rule of its own it fails alone, but in a list it takes the whole rule with it.
Conclusion
Separate the selectors with commas and the rule styles every element that matches any of them. Check each selector in the list, because one invalid selector switches off the whole rule.
Additional Resources
- Selector list, on MDN.
