The Next-Sibling Combinator
In each dish card, the price comes straight after the dish name. The owner wants a little space between the name and whatever follows it. You do not have to label the element that follows; you can describe it by where it sits.
The rule
+means "the very next sibling": same parent, immediately after.
A + B matches a B that comes immediately after an A, with the same parent. Only one element can be "the next" after any A.
<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>h3 + p {
margin-top: 0.25rem;
}In each card the order is h3, p.price, p. The element straight after each h3 is the price paragraph, so h3 + p matches the two prices, not the descriptions.
More readings on this page:
h2 + articlematches the omelette's card, the first thing after "Today's menu". The lassi card comes after another article, not after theh2.article + articlematches the lassi's card.h2 + pmatches only the opening hours. In the menu section, theh2is followed by anarticle, not ap..price + pmatches the two descriptions.
Siblings share a parent. The h2 in #menu and the paragraphs inside the cards are not siblings, because the paragraphs have a different parent.
Like all combinators, + adds nothing to specificity: h3 + p is (0,0,2).
The shortcut
Find each left-side element, then take exactly one step forward to its next sibling. If that one element matches the right side, it is selected. Never look two steps ahead or step backwards.
Find the left side
There are two
h2elements: one in#menu, one in#visit.
Drills
Answer fast — trust the rule.
Which elements does h3 + p match?
Which element does h2 + article match?
How many elements does article + article match?
Does .price + h3 match anything on the café page?
Remember
+selects the one element immediately after, with the same parent.- One step forward, never two, never backwards.
- Siblings must share a parent; children of a neighbour do not count.
