Next-Sibling Combinator: SelectorA + SelectorB
The next-sibling combinator (+) selects only the target sibling element that immediately follows the element matched by the preceding selector.
It uses the selectorA + selectorB syntax, where the plus sign (+) separates the two selectors and selects an element matching selectorB when it immediately follows an element matching selectorA.
(The elements matched by both selectors are children of the same parent element.)
Syntax
selectorA + selectorB {
/* ... */
}
Basic Example
h2 + p {
background-color: yellowgreen;
}
<article>
<h2>CSS Next-Sibling Combinator</h2>
<p>
Only the target sibling element that
immediately follows the preceding element is selected.
</p>
<p>Subsequent sibling elements are not selected.</p>
</article>
CSS Next-Sibling Combinator
Only the target sibling element that immediately follows the preceding element is selected.
Subsequent sibling elements are not selected.
Whitespace before and after the plus sign (+) separating the selectors is optional, but recommended for visual clarity.
/* Whitespace before and after the plus sign (+) is optional,
but recommended for visual clarity. */
h2+p { /* Not recommended */
background-color: yellowgreen;
}
h2 + p { /* Recommended */
background-color: yellowgreen;
}
Subsequent-Sibling Combinator: SelectorA ~ SelectorB
To select all target sibling elements that follow the preceding element, use the subsequent-sibling combinator (~).
Syntax
selectorA ~ selectorB {
/* ... */
}
Example
h2 ~ p {
background-color: yellowgreen;
}
<article>
<h2>CSS Subsequent-Sibling Combinator</h2>
<h3>h3</h3>
<p>
Selects all target sibling elements that follow the preceding element,
even if they do not immediately follow it.
</p>
<p>
Selects all target sibling elements that follow the preceding element,
even if they do not immediately follow it.
</p>
<div>
<p>Target elements that are not sibling elements are not selected.</p>
</div>
<p>
Selects all target sibling elements that follow the preceding element,
even if they do not immediately follow it.
</p>
</article>
CSS Subsequent-Sibling Combinator
h3
Selects all target sibling elements that follow the preceding element, even if they do not immediately follow it.
Selects all target sibling elements that follow the preceding element, even if they do not immediately follow it.
Target elements that are not sibling elements are not selected.
Selects all target sibling elements that follow the preceding element, even if they do not immediately follow it.
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
SelectorA + SelectorB
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
SelectorA + SelectorB
|
Selectors Level 4 #adjacent-sibling-combinators |
References
See Also
- CSS * Selector – Selects All Elements
- CSS Element Selector – Select by Element Name
- CSS Subsequent-Sibling Combinator (~) – Select All Subsequent Sibling Elements
- CSS Descendant Combinator – Select All Descendant Elements
- CSS Child Combinator (>) – Select All Direct Child Elements
- CSS Selector List (,) – Multiple Selectors Separated by Commas