Relationships in an article
Direct introduction.
Direct paragraph after the introduction.
Final paragraph in the article.
Compare depth and sibling relationships across three HTML scenarios and inspect exactly which elements each combinator selects.
.article pPreparing the scenario and its results…
Selects targets at any depth inside the container.
<article class="article" aria-label="Article structure about combinators">
<h3>Relationships in an article</h3>
<p class="lead">Direct introduction.</p>
<p>Direct paragraph after the introduction.</p>
<aside class="note">
<p>Paragraph nested inside a note.</p>
</aside>
<p>Final paragraph in the article.</p>
</article>.article p {
outline: 3px solid #f97316;
outline-offset: 3px;
background-color: color-mix(in oklch, #f97316 16%, transparent);
}CSS combinators guide
Distinguish depth, direct parentage, and sibling order without confusing the reference element with the selected element.
A combinator connects two compound selectors and requires their elements to hold a particular relationship in the document tree.
A combinator BIn A combinator B, the rule selects B. A provides the context B needs in order to match.
A B · A > BWhitespace allows any number of levels; > requires A to be the immediate parent of B.
A + B · A ~ B+ requires the next element sibling; ~ allows intervening siblings but never searches backwards.
Compare any descendant with children that depend directly on a container.
E FSelects B when it is found at any level inside an ancestor A.
Specificity(0, 0, 2).article p<article class="article">
Matches: <p>Matches</p>
<section>
Matches: <p>Matches</p>
</section>
</article>
<p>Does not match</p>.menu a<nav class="menu">
Matches: <a>Matches</a>
<span>
Matches: <a>Matches</a>
</span>
</nav>
<a>Does not match</a>E > FSelects B only when its immediate element parent matches A.
Specificity(0, 0, 2).article > p<article class="article">
Matches: <p>Matches</p>
<section>
<p>Does not match</p>
</section>
</article>.menu > a<nav class="menu">
Matches: <a>Matches</a>
<span>
<a>Does not match</a>
</span>
Matches: <a>Matches</a>
</nav>Compare the immediate element sibling with every matching sibling that appears later.
E + FSelects B when it shares a parent with A and is its next element sibling.
Specificity(0, 0, 2)h2 + p<section>
<h2>CSS</h2>
Matches: <p>Matches</p>
<p>Does not match</p>
</section>.current + a<nav>
<a class="current">Actual</a>
Matches: <a>Matches</a>
<span>
<a>Does not match</a>
</span>
</nav>E ~ FSelects every B after A when both share the same parent.
Specificity(0, 0, 2)h2 ~ p<section>
<h2>CSS</h2>
Matches: <p>Matches</p>
<aside>Nota</aside>
Matches: <p>Matches</p>
</section>.current ~ a<nav>
<a class="current">Actual</a>
Matches: <a>Matches</a>
<span>
<a>Does not match</a>
</span>
Matches: <a>Matches</a>
</nav>