Science, Tech, Math › Computer Science Grouping Multiple CSS Selectors Grouping CSS selectors simplifies your stylesheets Share Flipboard Email Print Science, Tech, Math PHP Programming Perl Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Jennifer Kyrnin Jennifer Kyrnin Writer University of California University of Washington Jennifer Kyrnin is a professional web developer who assists others in learning web design, HTML, CSS, and XML. Learn about our Editorial Process Updated on November 05, 2020 Reviewed by Jessica Kormos Reviewed by Jessica Kormos Saint Mary-of-the-Woods College Jessica Kormos is a writer and editor with 15 years' experience writing articles, copy, and UX content for Tecca.com, Rosenfeld Media, and many others. Learn about our Editorial Process 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. 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 elementAny element with the class of redAny element with an ID of subThe 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. Cite this Article Format mla apa chicago Your Citation Kyrnin, Jennifer. "Grouping Multiple CSS Selectors." ThoughtCo, Jul. 31, 2021, thoughtco.com/grouping-multiple-css-selectors-3467065. Kyrnin, Jennifer. (2021, July 31). Grouping Multiple CSS Selectors. Retrieved from https://www.thoughtco.com/grouping-multiple-css-selectors-3467065 Kyrnin, Jennifer. "Grouping Multiple CSS Selectors." ThoughtCo. https://www.thoughtco.com/grouping-multiple-css-selectors-3467065 (accessed May 29, 2023). copy citation Featured Video