Skip to content

CSS Tutorial CSS Selectors

CSS Sibling Selectors

Lesson 15 of 182 2 min read By Deepak Paswan Updated Oct 4, 2026

Siblings are elements that share the same parent. Two selectors let you style an element based on what comes before it.

Adjacent sibling: +

h2 + p matches a paragraph that comes immediately after an h2.

index.html
HTML
<style>
  h2 + p {
    font-size: 1.2em;
    color: #1a5276;
  }
</style>
<h2>Heading</h2>
<p>This is the first paragraph after the heading. It is styled.</p>
<p>This second paragraph is not.</p>

General sibling: ~

h2 ~ p matches every paragraph that follows the h2 at the same level, not just the first.

index.html
HTML
<style>
  h2 ~ p {
    border-left: 3px solid #d94a00;
    padding-left: 10px;
  }
</style>
<p>Before the heading: not matched.</p>
<h2>Heading</h2>
<p>After the heading: matched.</p>
<p>Also matched.</p>

Where this is handy

  • Add space only between neighbours: li + li { margin-top: 8px; } leaves the first item alone.
  • Style a form field’s message when the input is invalid, using the input as the starting point.
  • Show or hide something after a checkbox.
info
CSS only looks forward
Sibling selectors only select elements that come after the first one in the HTML. To style an element based on something that comes later, see :has() in Not, Is and Other Handy Selectors.