Selector grouping lets you apply the same CSS to multiple selectors at once by separating them with commas. It keeps stylesheets DRY and easier to maintain.
01
Comma ,
Separator.
02
One rule
Many targets.
03
h1, h2, p
Typography.
04
Mix types
Tag + class.
05
Not OR nest
Independent.
06
DRY CSS
Less repeat.
Fundamentals
Introduction
The CSS selector grouping pattern — written as element, element or selector1, selector2 — lets you apply the same styles to multiple elements simultaneously. It is one of the most practical techniques for keeping stylesheets clean and concise.
Instead of writing three separate rules with identical properties, you list every selector separated by commas in a single rule block. Every matching element receives the same declarations.
Definition and Usage
Separate selectors with a comma. There is no space required after the comma, though h1, h2, p with spaces is the conventional readable format. Each selector in the list is evaluated independently — if an element matches any one of them, the styles apply.
💡
Beginner Tip
Do not confuse h1, p (grouping — style all h1 and all p) with h1 p (descendant — style p elements inside h1, which is invalid for block headings anyway). The comma means “or” between separate selectors.
The first grouped rule sets shared spacing for all three regions. A second group adds background and border only to header and footer, leaving main with a clean content area.
Tips
💬 Usage Tips
Group similar elements — Combine selectors that genuinely share the same visual treatment.
Split when styles diverge — Do not force unrelated elements into one group if they need different values.
One property per line — Keep grouped rule blocks readable for future edits.
Layer specificity — Follow grouped base rules with more specific overrides when needed.
Group pseudo-classes too — Patterns like a:focus, button:focus reduce duplication for accessibility.
Watch Out
⚠️ Common Pitfalls
Comma vs space — div, p groups selectors; div p targets nested paragraphs.
One-size-fits-all styles — Shared padding on h1, p may look wrong; headings often need different spacing.
Specificity overrides — A later, more specific rule can undo your grouped styles for one element only.
Over-grouping — Cramming unrelated selectors together makes maintenance harder when one element needs to change.
Invalid trailing comma — h1, h2, without a following selector is a CSS syntax error.
A11y
♿ Accessibility
Focus styles — Group a:focus-visible, button:focus-visible for consistent keyboard indicators.
Color contrast — Ensure grouped text colors meet WCAG contrast on all targeted elements.
Semantic HTML — Grouping does not replace using the correct element for its role.
Do not hide content — Avoid grouped rules that remove focus outlines or make text unreadable.
Touch targets — When grouping buttons and links, verify minimum tap size on mobile.
🧠 How Selector Grouping Works
1
Parse selector list
The browser splits the comma-separated list into individual selectors.
Parse
2
Match each selector
Every selector in the list is evaluated against the DOM independently.
Match
3
Apply shared styles
All elements matching any selector in the list receive the same declarations.
Style
=
📝
DRY stylesheet
One rule updates many elements at once, keeping CSS maintainable.
Compatibility
🖥 Browser Compatibility
Comma-separated selector lists are supported in all browsers and have been a core CSS feature since CSS1.
✓ Baseline · Universal support
Grouping everywhere
Selector lists work reliably in Chrome, Firefox, Safari, Edge, and Opera.
99%Universal support
Google ChromeAll versions
Full support
Mozilla FirefoxAll versions
Full support
Apple SafariAll versions
Full support
Microsoft EdgeAll versions
Full support
OperaAll modern versions
Full support
Selector grouping (comma lists)99% supported
Bottom line: Comma-separated selector lists are safe for all projects.
Wrap Up
🎉 Conclusion
Selector grouping is an efficient way to apply common styles to multiple HTML elements in a single rule. By listing selectors separated by commas, you streamline your CSS and make future updates faster.
Use grouping whenever several selectors genuinely share the same properties, but split rules when elements need different spacing, colors, or layout values.
Mix element, class, and attribute selectors when needed
Keep grouped rules focused and short
❌ Don’t
Confuse comma with descendant space
Force unrelated elements into one group
Apply identical padding to h1 and p blindly
Leave trailing commas in selector lists
Forget that specificity can override groups
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about grouping
Use these points when writing comma-separated selectors.
5
Core concepts
,01
Comma
Separator.
Syntax
OR02
Independent
Not nested.
Logic
h1,p03
Mix tags
One rule.
Pattern
DRY04
Less copy
Maintain.
Benefit
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
Selector grouping uses commas to list multiple selectors in one rule. The same CSS declarations apply to every selector in the list — for example, h1, h2, p { color: navy; } styles all three element types.
No. A comma separates independent selectors; it does not create a relationship between them. div, p means "style all div elements AND all p elements" — not "p inside div".
Yes. Rules like button, .btn, input[type="submit"] { font-weight: 600; } are valid and commonly used to share button styling across different HTML patterns.
Each selector in the group is evaluated separately. An element matches if it satisfies any one selector in the list. More specific rules elsewhere can still override the grouped rule for individual elements.
Yes. Comma-separated selector lists have been supported in every browser since the earliest days of CSS and are safe to use everywhere.