Course contentsShow
← Web Development
Lesson 124 of 2,9823. CSS FundamentalsFree lesson

Combinators: Adjacent and General Sibling

Use + and ~ to select elements based on their sibling relationships in the DOM tree.

Combinators: Adjacent and General Sibling

What you'll learn: How to select sibling elements that come after a specific element using the + and ~ combinators.

The Core Idea

In HTML, elements at the same level sharing the same parent are called siblings. CSS gives you two combinators to target siblings that appear after (not before) a specific element:

  • Adjacent Sibling (+): Selects only the very next sibling immediately following the element
  • General Sibling (~): Selects all siblings that come after the element

Think of it like seats in a row at a theater. The adjacent sibling combinator (+) is like saying "the person in the seat directly next to me." The general sibling combinator (~) is like saying "anyone sitting to my right, no matter how far down the row."

How They Work

Adjacent Sibling (+)

h2 + p {
  font-weight: bold;
}

This makes only the first paragraph immediately after an <h2> bold—no other paragraphs are affected.

General Sibling (~)

h2 ~ p {
  color: gray;
}

This makes all paragraphs after an <h2> (at the same level) gray, not just the first one.

Important Notes

  • Both combinators only look forward (down the HTML), never backward
  • Elements must share the same parent to be siblings
  • Commonly used for styling elements based on context, like paragraphs after headings

Key Takeaway: Use + to target the single next sibling and ~ to target all following siblings at the same level in the DOM tree.