Skip to the workspace
Pebellslearn by building
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.

css / The Subsequent-Sibling Combinator

Explore each scene at your own pace.

01 · The rule

The rule

The Subsequent-Sibling Combinator

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

Match later siblings while keeping the shared-parent requirement visible.

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
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.