🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Sibling Selectors

AI & DATA SCIENCE // siblings-selectors

Sibling combinators target an element based on its relationship to a preceding sibling: + matches the very next sibling, while ~ matches every later sibling sharing the same parent.

Syntax

former + target {
  property: value;
}
former ~ target {
  property: value;
}

Deep Dive Course

h2 + p, using the adjacent sibling combinator +, matches a <p> element only if it comes immediately after an <h2> at the same nesting level, with nothing else in between — a classic use case being adding extra top margin only to the first paragraph directly following a heading, leaving subsequent paragraphs unaffected. h2 ~ p, using the general sibling combinator ~, instead matches every <p> that comes anywhere after that <h2> among its siblings, not just the immediately adjacent one, however many other elements separate them, as long as they share the same parent.

1Understanding Sibling Selectors

h2 + p, using the adjacent sibling combinator +, matches a <p> element only if it comes immediately after an <h2> at the same nesting level, with nothing else in between — a classic use case being adding extra top margin only to the first paragraph directly following a heading, leaving subsequent paragraphs unaffected. h2 ~ p, using the general sibling combinator ~, instead matches every <p> that comes anywhere after that <h2> among its siblings, not just the immediately adjacent one, however many other elements separate them, as long as they share the same parent.

💡

Use the adjacent sibling combinator (+) specifically for a common pattern like removing top margin from an element only when it immediately follows another specific element, like the first paragraph right after a heading, leaving later paragraphs with their normal spacing.

editor.html
h2 + p {
  margin-top: 0;
}
localhost:3000

2Practical Example

Here is a real-world application of Sibling Selectors showing how it is used in production CSS code.

editor.html
h2 ~ p {
  color: gray;
}

<h2>Title</h2>
<p>First</p>
<p>Second</p>
localhost:3000

3Best Practices

Follow these guidelines when working with Sibling Selectors:

1. Use + (adjacent sibling) for a rule that should apply only to the very next element immediately following a specific sibling, with nothing in between

2. Use ~ (general sibling) when a rule should apply to every later sibling after a specific one, not just the immediately adjacent one

3. Remember both sibling combinators only match elements sharing the same parent — they never reach into a different nesting level or a different parent's children

⚠️

Tip: Use the adjacent sibling combinator (+) specifically for a common pattern like removing top margin from an element only when it immediately follows another specific element, like the first paragraph right after a heading, leaving later paragraphs with their normal spacing.

editor.html
h2 + p {
  margin-top: 0;
}
localhost:3000

Examples

Example 01Basic Usage
h2 + p {
  margin-top: 0;
}
Example 02Advanced Example
h2 ~ p {
  color: gray;
}

<h2>Title</h2>
<p>First</p>
<p>Second</p>

Best Practices

  • Use + (adjacent sibling) for a rule that should apply only to the very next element immediately following a specific sibling, with nothing in between
  • Use ~ (general sibling) when a rule should apply to every later sibling after a specific one, not just the immediately adjacent one
  • Remember both sibling combinators only match elements sharing the same parent — they never reach into a different nesting level or a different parent's children

Interview Question

Why does h2 + p only ever match at most one paragraph after a given heading, no matter how many paragraphs actually follow it in the markup?

Hint: Think about exactly what 'immediately adjacent' means — does it describe a relationship that can hold true for more than one element relative to the same heading?

The adjacent sibling combinator specifically matches an element that comes immediately, directly next, after the specified former sibling, with absolutely nothing else in between them at the same nesting level — by definition, only one single element can occupy that immediately-next position relative to any given element, since immediately-next describes one specific, unique position in the sibling sequence, not a range of positions. Once a second paragraph follows the first one, that second paragraph is immediately adjacent to the first paragraph, not to the original heading, so h2 + p simply doesn't match it at all, regardless of how close it might be to the heading in absolute terms. This is exactly the structural distinction between + and ~: + is inherently limited to matching at most one single element, the one genuinely immediately following the reference element, while ~ deliberately extends that matching to every qualifying sibling that comes afterward, however many there are.

Exercises

MediumPractice using Sibling Selectors in a real scenario.
View Solution
h2 + p {
  margin-top: 0;
}

Frequently Asked Questions

Why does h2 + p only ever match at most one paragraph after a given heading, no matter how many paragraphs actually follow it in the markup?

The adjacent sibling combinator specifically matches an element that comes immediately, directly next, after the specified former sibling, with absolutely nothing else in between them at the same nesting level — by definition, only one single element can occupy that immediately-next position relative to any given element, since immediately-next describes one specific, unique position in the sibling sequence, not a range of positions. Once a second paragraph follows the first one, that second paragraph is immediately adjacent to the first paragraph, not to the original heading, so h2 + p simply doesn't match it at all, regardless of how close it might be to the heading in absolute terms. This is exactly the structural distinction between + and ~: + is inherently limited to matching at most one single element, the one genuinely immediately following the reference element, while ~ deliberately extends that matching to every qualifying sibling that comes afterward, however many there are.

Related Functions

Child-SelectorsDescendant-SelectorsCSS-Rules