Sibling combinators
Definition and purpose
Sibling combinators match elements that share a parent. The next-sibling combinator (+) selects a following element immediately after another element sibling. The subsequent-sibling combinator (~) selects later matching siblings, even with intervening elements.
Immediate paragraph
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>h2 + p { color: navy; }</style> </head> <body> <section> <h2>News</h2> <!-- note --> <p id="a">Lead</p> <p id="b">Detail</p> </section> </body></html>Only the lead paragraph is the next element sibling.
Syntax and mental model
h2 + p matches a paragraph immediately following an h2 under the same parent. h2 ~ p matches every later paragraph sibling. Text nodes and comments do not interrupt element adjacency. Neither combinator searches inside a later sibling or selects earlier siblings. The right-hand selector identifies the target.
All later matching siblings
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>h2 ~ p { color: green; }</style> </head> <body> <section> <p id="a">Before</p> <h2>News</h2> <p id="b">Lead</p> <div> <p id="c">Nested</p> </div> <p id="d">Detail</p> </section> </body></html>Lead and Detail match, not the earlier or nested paragraph.
Common mistakes
Do not assume ~ selects every later element on the page. It is restricted to siblings sharing the same parent, and + is restricted to the immediately following element.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Descendant and child combinators. Next lesson: Attribute selectors.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Sibling combinators — knowledge check
Five questions on sibling combinators.
Which selects a paragraph immediately after h2?
What must h2 and p share for h2 ~ p to match?
Does a comment between h2 and p break h2 + p?
Does h2 ~ p select a p before the h2?
Which selects later matching siblings even after an intervening div?