Adjacent sibling selector in CSS

The adjacent sibling selector + allows you to select an element by its adjacent sibling above.

Syntax

cssselector1 + selector2 {
  
}

Example

Let's select all the p tags that immediately follow the h2 tags and color them red:

html<h2>text</h2>
<p>
  +
</p>
<p>
  -
</p>
<p>
  -
</p>
cssh2 + p {
  color: red;
}

Результат:

Example

Let's select all p tags immediately after elements with the class .test and color them red:

html<p class="test">
  text
</p>
<p>
  +
</p>
<p>
  -
</p>
css.test + p {
  color: red;
}

Результат:

Example

Let's select all elements with the class .elem immediately after elements with the class .test and color them red:

html<p class="test">
  text
</p>
<p class="elem">
  +
</p>
<p>
  -
</p>
css.test + .elem {
  color: red;
}

Результат:

See also