Adjacent sibling

Avatar of Sara Cope
Sara Cope on (Updated on )

DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!

The adjacent sibling combinator in CSS isn’t a selector on its own, but a way of combining two selectors. For example:

p + p {
  margin: 0;
}

The plus sign (+) is the adjacent sibling combinator, between two paragraph tag (element) selectors. What this means is “select any paragraph tag that is directly after another paragraph tag (with nothing in between)”. Here’s some examples of what it would select:

<div>
  <p>I'm a paragraph</p>
  <p>I get selected!</p>
</div>

<div>
  <p>I'm a paragraph</p>
  <h2>Monkey hair</h2>
  <p>I will NOT get selected</p>
</div>

This is mostly useful for when using semantic markup and needing to adjust for certain scenarios in which elements are directly next to each other.

See the Pen Adjacent Sibling Selector by Sara Cope (@saracope) on CodePen.

More Resources

Browser Support

Chrome Safari Firefox Opera IE Android iOS
Any Any Any Any 7+ Any Any