ProgramGuru

Universal and Grouping Selectors

Universal and Grouping Selectors

Use the universal selector to match elements broadly and grouping syntax to share a rule.

What you’ll learn

  • Explain what * matches.
  • Share declarations with comma-separated selectors.
  • Distinguish a selector list from a descendant selector.

Understand the concept

* is the universal selector: it matches elements of any tag name in its context. It is often used with box-sizing in a global reset, but broad global rules deserve care.

Commas create a selector list. For example, h2, h3 matches both heading types. This reduces repeated declarations and keeps shared design decisions in one place.

What the browser evaluates

The comma separates independent selectors. Without a comma, <code>h2 h3</code> means an h3 inside an h2, which is different.

Group headings

Share a color without repeating a second declaration block.

HTML

<h3>Today</h3><h4>Tomorrow</h4><p>Weekly outlook</p>

CSS

h3, h4 { color: #7c3aed; }

Rendered output

Today

Tomorrow

Weekly outlook

Observe: Both headings change color while the paragraph stays unchanged.

Scoped universal selector

Match each child inside this small box.

HTML

<section class="example-box"><h3>Team</h3><p>Two updates</p><span>New</span></section>

CSS

.example-box > * { border-bottom: 1px dashed #cbd5e1; padding: 6px; }

Rendered output

Team

Two updates

New

Observe: Every direct child has a divider, even though their tag names differ.

Explain the result

The comma separates independent selectors. Without a comma, <code>h2 h3</code> means an h3 inside an h2, which is different.

Compare the two examples: identify the selector, each changed property, and which visible part of the output proves that the CSS took effect.

Common mistakes to avoid

  • The universal selector can affect more elements than you intend; keep broad rules purposeful.
  • A comma makes a list; a space means a descendant relationship.

Try it yourself

Group h3 and p in a single rule to give both a consistent line-height.

Before changing the code, predict what will change. Then verify by checking the rendered result.

Check your understanding

What does the selector list h3, h4 match?
Choose one answer. You can try again.

Answer and explanation

Both h3 and h4

The comma creates two independent selectors.

What you should remember

Use grouping for shared declarations and the universal selector with a clear scope.

Revisit the rendered examples if the rule still feels abstract. Change one declaration at a time so you can connect cause and effect.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Descendant, Child, and Sibling Selectors →