Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Combinator Selectors  ›  Capstone Project

The Subsequent-Sibling Combinator

Subsequent Sibling 15 minThe tilde combinator matches every later sibling that shares a parent with the first element, not only the next one.

The Subsequent-Sibling Combinator

The next-sibling combinator reaches exactly one element. Sometimes you want every element that follows, such as every card after a section heading, however many cards the menu grows to. The tilde does that.

The rule

~ means "any later sibling": same parent, anywhere after.

A ~ B matches every B that comes somewhere after an A and shares its parent. It does not have to be immediately after.

<main>
  <section id="menu">
    <h2>Today's menu</h2>
    <article class="dish">
      <h3>Masala omelette</h3>
      <p class="price">₹180</p>
      <p>Three eggs, green chilli and coriander.</p>
    </article>
    <article class="dish special">
      <h3>Mango lassi</h3>
      <p class="price">₹120</p>
      <p>Thick, cold and only in summer.</p>
    </article>
  </section>
  <section id="visit">
    <h2>Visit us</h2>
    <p>Open 8 am to 6 pm, every day.</p>
  </section>
</main>
h2 ~ article {
  margin-top: 1.5rem;
}

h2 ~ article matches both cards, because each comes after the menu's h2 in the same section. Compare with h2 + article, which reaches only the first.

More readings:

  • h3 ~ p matches all four card paragraphs: in each card, both paragraphs come after the h3.
  • .price ~ p matches the two descriptions.
  • .special ~ article matches nothing: the lassi's card is the last article, and ~ never looks backwards.
  • h2 ~ p matches only the opening hours. The card paragraphs are inside articles, so they are not siblings of the h2.

Specificity, as with every combinator, only counts the selectors: h2 ~ article is (0,0,2).

Here are all four combinators side by side, for an A and a B:

Written Reads as Relationship
A B B anywhere inside A descendant
A > B B directly inside A child
A + B B right after A next sibling
A ~ B B somewhere after A subsequent sibling

The shortcut

Stand on the left-side element and look forward along its row. Everything after it with the same parent is a candidate; keep the ones that match the right side. Nothing behind you, and nothing inside other elements, counts.

Which elements does `.dish ~ .dish` match?Step 1 of 4
  1. Stand on each left match

    The omelette card and the lassi card are both .dish.

Drills

Answer fast — trust the rule.

quick drill

How many elements does h2 ~ article match?

quick drill

How many elements does h3 ~ p match?

quick drill

Which elements does .special ~ article match?

quick drill

Which selector matches both dish descriptions and nothing else?

quick drill

How many elements does h2 ~ p match?

Remember

  • ~ selects every later sibling that matches, not just the next one.
  • Stand on the left element and look forward along its row only.
  • Four combinators: space inside, > child, + next, ~ any later.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFour ways to relate

Using the café page, give one selector for each combinator (descendant, child, next sibling, subsequent sibling) that selects something useful, and explain in one sentence each what it matches and why.