Skip to the workspace
Pebellslearn by building
Simple Selectors  ›  Lesson

Grouping Selectors

grouping 12 minA comma-separated selector list applies one declaration block to several independent selectors, each keeping its own specificity, and one invalid item drops the whole rule.

css / Grouping Selectors

Explore each scene at your own pace.

01 · The rule

The rule

Grouping Selectors

A comma-separated selector list applies one declaration block to several independent selectors, each keeping its own specificity, and one invalid item drops the whole rule.

Share declarations across selector alternatives and inspect invalid-selector behavior.

The café's h1, h2 and h3 should all use the same serif font. You could write three identical rules, then remember to edit all three whenever the font changes. Or you could write one rule with a list of selectors.

The rule

A comma means "and also": one block, several independent selectors.

h1,
h2,
h3 {
  font-family: Georgia, serif;
  color: navy;
}

Every example uses the Harbour Café page from the first lesson of the course: a header with an h1 and a nav of two links, a section#menu holding an h2 and two article.dish cards (the second is also .special), each with an h3, a p.price and a description p, then a section#visit with an h2 and one p, and a footer with one p.

A selector list is several selectors separated by commas. The declarations apply to anything matched by any of them. Writing each selector on its own line keeps long lists readable.

Each item in the list is independent:

  • Each keeps its own specificity. In h2, #visit p, the h2 headings get the declarations at (0,0,1), not at the id's weight.
  • The comma is not a space. h2, .price selects headings and prices; h2 .price selects prices inside headings, which on this page is nothing.

One sharp edge: if any selector in a plain list is invalid, the browser drops the whole rule. A typo in one item switches off the styles for all the others. (The newer :is() and :where() wrappers are forgiving about this, but a bare comma list is not.)

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.