offline version v3


289 of 298 menu

Adjacent sibling selector in CSS

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

Syntax

selector1 + selector2 { }

Example

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

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

:

Example

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

<p class="test"> text </p> <p> + </p> <p> - </p> .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:

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

:

See also

enru