Grouping Multiple CSS Selectors

Grouping CSS selectors simplifies your stylesheets

When you group CSS selectors, you apply the same styles to several different elements without repeating the styles in your stylesheet. Instead of having two, three, or more CSS rules that do the same thing (set the color of something to red, for example), you use a single CSS rule that accomplishes the same thing. The secret to this efficiency-boosting tactic is the comma.

Male office worker at workstation, view over shoulder
Christopher Robbins/Photodisc/Getty Images 

How to Group CSS Selectors

To group CSS selectors in a style sheet, use commas to separate multiple grouped selectors in the style. In this example, the style affects the p and div elements:

div, p { color: #f00; }

In this context, a comma means "and," so this selector applies to all paragraph elements and all division elements. If the comma were missing, the selector would instead apply to all paragraph elements that are a child of a division. That is a different kind of selector, so the comma is important.

You can group any form of selector with any other selector. This example groups a class selector with an ID selector:

p.red, #sub { color: #f00; }

This style applies to any paragraph with the class attribute of red and any element (because the kind is not specified) with an ID attribute of sub.

You can group any number of selectors, including selectors that are single words and compound selectors. This example includes four different selectors:

p, .red, #sub, div a:link { color: #f00; }

This CSS rule would apply to:

  • Any paragraph element
  • Any element with the class of red
  • Any element with an ID of sub
  • The link pseudo class of the anchor elements that are descendants of a division.

That last selector is a compound selector. As shown, it's easily combined with the other selectors in this CSS rule. The rule sets the color of #f00 (red) on these four selectors, which is preferable to writing four separate selectors to achieve the same result.

Any Selector Can Be Grouped

You can place any valid selector in a group, and all elements in the document that match all the grouped elements will have the same style based on that style property.

Some designers prefer to list the grouped elements on separate lines for legibility in the code. The appearance on the website and the load speed remains the same. For example, you can combine styles separated by commas into one style property in one line of code:

th, td, p.red, div#firstred { color: red; }

or you can list the styles on individual lines for clarity:

th,
td,
p.red,
div#firstred
{
color: red;
}

Why Group CSS Selectors?

Grouping CSS selectors helps minimize the size of your stylesheet so it loads faster Admittedly, style sheets are not the main culprits in slow loading; CSS files are text files, so even very long CSS sheets are tiny when compared to unoptimized images. Still, every bit of optimization helps, and if you can shave some size off your CSS and load the pages that much faster, that's a good thing.

Grouping selectors also makes site maintenance far easier. If you need to make a change, you can simply edit a single CSS rule instead of multiple ones. This approach saves time and hassle.

The bottom line: Grouping CSS selectors boosts efficiency, productivity, organization, and in some cases, even load speed.