Encodera

Thanks for reading!

Donate to the site — we work hard for you.

HTML & CSS reference

A + B

Sibling combinators: `+` selects the element right after A, `~` selects all following siblings of A.

Syntax

A + B { … }  A ~ B { … }

Parameters

ParameterTypeDescription
A + Bnext siblingB that comes immediately after A with the same parent.
A ~ Blater siblingsEvery B that comes after A with the same parent.

Examples

The paragraph right after a heading:

<style>
  h3 + p { font-weight: bold; }
</style>
 
<h3>Title</h3>
<p>The first paragraph after the title is bold.</p>
<p>The second one is not.</p>

All paragraphs after the heading:

<style>
  h3 ~ p { color: teal; }
</style>
 
<p>Before the title — not selected.</p>
<h3>Title</h3>
<p>After the title.</p>
<p>Also after the title.</p>

Notes

Both combinators only look forward: there is no "previous sibling" combinator. To style an element based on what comes after it, use :has(), for example h3:has(+ p).

See also

  • A > B — The child combinator: selects elements B that are direct children of A.
  • :has() — Matches an element that contains something matching the selector in brackets — the "parent selector".

In the tutorial

  • Selectors — How to pick elements for styling — by tag, class, id, nesting and attribute.

Thanks for reading!

Donate to the site — we work hard for you.