CSS Selector Grouping (,)

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Selector Lists

What You’ll Learn

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.

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.

📝 Syntax

The syntax for selector grouping is:

syntax.css
selector1, selector2, selector3 {
  /* CSS properties */
}

List any valid CSS selectors separated by commas. The declarations inside the braces apply to every selector in the list.

Basic Example

grouping-basic.css
h1, h2, p {
  color: #1e3a8a;
  font-family: system-ui, sans-serif;
}

button {
  background: #dbeafe;
  border: none;
  padding: 0.5rem 0.85rem;
  cursor: pointer;
}

h1:hover, h2:hover {
  color: #1d4ed8;
}

Comma vs Space

h1, p — Grouping Styles every <h1> and every <p> on the page independently.
h1 p — Descendant Styles only <p> elements nested inside an <h1>.

Syntax Rules

  • Separate selectors with commas; spaces after commas improve readability.
  • Each selector in the list is independent — no parent-child relationship is created.
  • You can mix element, class, ID, attribute, and pseudo-class selectors.
  • Trailing commas are invalid in CSS (unlike JavaScript arrays).
  • More specific rules elsewhere can override grouped styles for individual elements.

Related Topics

h1, h2, p button, .btn h1:hover, h2:hover header, footer, main

⚡ Quick Reference

QuestionAnswer
Separator, (comma)
Also calledSelector list / grouping
MeaningApply same styles to multiple selectors
Common patternh1, h2, h3 { font-family: serif; }
Not the same ash1 p (descendant with a space)
Browser supportAll browsers

When to Use Selector Grouping

Comma grouping is ideal whenever multiple selectors share identical styles:

  • Typography resets — Apply the same font and color to h1, h2, h3, p.
  • Button systems — Style button, .btn, input[type="submit"] together.
  • Layout regions — Share padding on header, footer, main.
  • Interactive states — Group a:hover, button:hover for consistent hover color.
  • Form fields — Unify borders on input, textarea, select.

👀 Live Preview

h1, h2, p share navy text and the same font family. The button uses its own separate rule:

Welcome to My Blog

This is a sample paragraph.

Blog Post Title

Another sample paragraph in the blog.

Examples Gallery

Practice selector grouping with typography, buttons, hover states, and layout regions.

📜 Core Patterns

Apply one rule block to multiple selectors.

Example 1 — Shared typography for headings and paragraphs

Style h1, h2, and p with the same color and font in one rule.

group-typography.css
h1, h2, p {
  color: #1e3a8a;
  font-family: system-ui, sans-serif;
}

button {
  background: #dbeafe;
  border: none;
  padding: 0.5rem 0.85rem;
  cursor: pointer;
}
Try It Yourself

How It Works

One rule styles all three text elements. The button is excluded because it needs different background and padding values in its own rule.

Example 2 — Mixed button selectors

Group button, .btn, and submit inputs to share button styling.

group-buttons.css
button, .btn, input[type="submit"] {
  display: inline-block;
  padding: 0.5rem 1rem;
  background: #2563eb;
  color: #fff;
  border: none;
  border-radius: 0.4rem;
  font-weight: 600;
  cursor: pointer;
}
Try It Yourself

How It Works

Different HTML patterns for clickable actions share one visual style. Grouping avoids copying the same six properties into three separate rules.

📄 States & Layout

Group pseudo-classes and semantic layout elements.

Example 3 — Grouped hover states

Apply the same hover color to multiple heading levels with h1:hover, h2:hover.

group-hover.css
h1, h2 {
  color: #1e3a8a;
  cursor: pointer;
  transition: color 0.2s;
}

h1:hover, h2:hover {
  color: #1d4ed8;
}
Try It Yourself

How It Works

Base styles are grouped for h1, h2. Hover styles are grouped separately for h1:hover, h2:hover so both heading levels darken on pointer-over.

Example 4 — Layout region padding

Share horizontal padding across semantic layout landmarks.

group-layout.css
header, footer, main {
  padding: 1rem 1.5rem;
  max-width: 48rem;
  margin: 0 auto;
}

header, footer {
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
}
Try It Yourself

How It Works

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.

💬 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.

⚠️ Common Pitfalls

  • Comma vs spacediv, 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 commah1, h2, without a following selector is a CSS syntax error.

♿ 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.

🖥 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 Chrome All versions
Full support
Mozilla Firefox All versions
Full support
Apple Safari All versions
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
Selector grouping (comma lists) 99% supported

Bottom line: Comma-separated selector lists are safe for all projects.

🎉 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.

💡 Best Practices

✅ Do

  • Group selectors with shared styles
  • Use spaces after commas for readability
  • Split base and :hover groups logically
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about grouping

Use these points when writing comma-separated selectors.

5
Core concepts
OR 02

Independent

Not nested.

Logic
h1,p 03

Mix tags

One rule.

Pattern
DRY 04

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.

Practice in the Live Editor

Open the HTML editor and experiment with h1, h2, p, mixed button groups, and layout region selectors.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful