Skip to main content

How to Apply One CSS Rule to Several Selectors

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<selector>, <selector> {
<declarations>
}
PartWhat 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:

index.html, an HTML page with a style element holding one rule for h1 and h2 that sets the color 5b5fc7, and a body with the heading Team handbook, the subheading Holidays and one paragraph.

index.html
<!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 page in Chromium: the heading Team handbook and the subheading Holidays in purple, and the paragraph Ask your manager before you book. in black.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • Both headings are purple. Chromium gave the h1 and the h2 the color rgb(91, 95, 199), which is #5b5fc7.
  • The paragraph stayed black, because p isn't in the list.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the comma: h1 h2Without the comma, the space means "an h2 inside an h1", so neither heading changedSeparate 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 blackRemove the last comma

Troubleshooting​

SymptomLikely causeFix
None of the selectors in the list workOne selector in the list is invalid, and that drops the whole rule: with h1, h2, p:hovering, nothing changed colorFix or remove the invalid selector, or give it a rule of its own
Only some of the elements changeThe others aren't in the list, or their selector is misspelledAdd 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​