Skip to content

Courses / CSS3 Mastery: Beginner to Advanced / Grouping selectors

2,087 people viewed this

Grouping selectors

CSS Grouping and Combinators

Learning Objectives

  • Group selectors to reduce repetition.
  • Understand descendant and child combinators.
  • Use adjacent and subsequent sibling combinators.
  • Understand how HTML relationships affect matching.

1. Grouping Selectors

Separate selectors with commas when they share declarations.

h1, h2, h3 {
  font-family: Arial, sans-serif;
  color: navy;
}

2. Descendant Combinator

A space matches an element nested anywhere inside another element.

.article p { line-height: 1.7; }

This matches paragraphs inside an element with the article class, including deeply nested paragraphs.

3. Child Combinator

The greater-than symbol matches direct children only.

.menu > li { list-style: none; }

4. Adjacent Sibling Combinator

The plus symbol matches the immediately following sibling.

h2 + p { margin-top: 0; }

5. Subsequent Sibling Combinator

The tilde symbol matches following siblings that share the same parent.

h2 ~ p { color: #555; }

Complete Example

<div class="article">
  <h2>Introduction</h2>
  <p>First paragraph</p>
  <div>
    <p>Nested paragraph</p>
  </div>
  <p>Another paragraph</p>
</div>

In this example, .article p matches all three paragraphs. .article > p matches only the two direct paragraph children. h2 + p matches the first paragraph after the heading. h2 ~ p matches following paragraph siblings of that heading.

Practice Exercise

Create a navigation list and an article. Use all four combinators, then change the HTML nesting and predict which elements stop matching.

Rate this course

No ratings yet — yours can be the first.

Stars show on this course. Your written note stays private.

Your stars