Grouping & Universal

Style several selectors at once and target everything.

Syntaxh1, h2, h3 { margin: 0; } * { box-sizing: border-box; }

To apply the same styles to multiple selectors, separate them with commas. This is a grouping selector.

The universal selector * matches every element. It is often used to reset default spacing.

Example

Try it yourself
Loading editor…
Press Run to see the result.

When to use it

  • A developer groups h1, h2, h3 selectors to share a single font-family declaration, avoiding three duplicate rules.
  • A designer uses the universal selector * to reset margin and padding on all elements before applying the site's layout styles.
  • A developer groups .header, .footer, .sidebar to apply the same background color, reducing stylesheet repetition.

More examples

Grouped selector sharing styles

Applies the same font and color declarations to four heading levels with a single grouped rule.

Example Β· css
h1, h2, h3, h4 {
  font-family: Georgia, serif;
  color: #1a1a2e;
  line-height: 1.2;
}

Universal selector reset

Uses the * universal selector to zero out margins and padding on every element as a CSS reset foundation.

Example Β· css
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

Grouping layout sections

Groups multiple section class names to share padding and centering styles without repeating declarations.

Example Β· css
.hero, .features, .pricing {
  padding: 64px 24px;
  max-width: 1200px;
  margin-inline: auto;
}

Discussion

  • Be the first to comment on this lesson.
Grouping & Universal β€” CSS | SoundsCode