Subsequent-Sibling Combinator: SelectorA ~ SelectorB
The subsequent-sibling combinator (~) selects all target sibling elements that follow the element matched by the preceding selector.
It uses the selectorA ~ selectorB syntax, where the tilde (~) separates the two selectors and selects all elements matching selectorB that follow 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 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.
Whitespace before and after the tilde (~) separating the selectors is optional, but recommended for visual clarity.
/* Whitespace before and after the tilde (~) is optional,
but recommended for visual clarity. */
h2~p { /* Not recommended */
background-color: yellowgreen;
}
h2 ~ p { /* Recommended */
background-color: yellowgreen;
}
Next-Sibling Combinator: SelectorA + SelectorB
To select only the target sibling element that immediately follows the preceding element, use the next-sibling combinator (+).
Syntax
selectorA + selectorB {
/* ... */
}
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.
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
SelectorA ~ SelectorB
|
1 | 12 | 1 | 3 |
Specifications
| Specification | |
|---|---|
SelectorA ~ SelectorB
|
Selectors Level 4 #general-sibling-combinators |
References
See Also
- CSS * Selector – Selects All Elements
- CSS Element Selector – Select by Element Name
- CSS Next-Sibling Combinator (+) – Select the Next Sibling Element
- CSS Descendant Combinator – Select All Descendant Elements
- CSS Child Combinator (>) – Select All Direct Child Elements
- CSS Selector List (,) – Multiple Selectors Separated by Commas