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 ~ pmatches all four card paragraphs: in each card, both paragraphs come after theh3..price ~ pmatches the two descriptions..special ~ articlematches nothing: the lassi's card is the last article, and~never looks backwards.h2 ~ pmatches only the opening hours. The card paragraphs are inside articles, so they are not siblings of theh2.
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 |