Articles

CSS Sibling Combinators: + and ~ Selectors Explained

CSS sibling combinators select elements by their neighbors: + targets the next sibling, ~ targets all later siblings. Syntax, examples, and limits.

Takina Takina · · 5 min read
Close-up of CSS code on a screen

CSS sibling combinators select an element based on the element that comes before it under the same parent. The next-sibling combinator (+) matches only the element immediately after another, and the subsequent-sibling combinator (~) matches every later sibling. They let you style things like “the paragraph right after a heading” or “every field after a checked checkbox” without adding classes to the markup.

The four CSS combinators

Combinators describe a relationship between two selectors. CSS has four:

CombinatorSyntaxMatches
DescendantA BAny B nested anywhere inside A
ChildA > BB that is a direct child of A
Next siblingA + BB immediately after A, same parent
Subsequent siblingA ~ BAny B after A, same parent

The first two look down the tree. The two sibling combinators look sideways, and only forward: they match elements that come after the reference element in source order, never before it.

The next-sibling combinator (+)

A + B matches a B element only if it directly follows an A element and both share the same parent. Text between them does not count, but any element in between breaks the match.

h2 + p {
  margin-top: 0;
  font-size: 1.125rem;
}

This styles the first paragraph after each h2 as a lead-in, while later paragraphs stay normal. If an image sat between the heading and the paragraph, the rule would not apply.

A classic use is the “lobotomized owl” spacing pattern, which adds space between adjacent elements without adding it before the first one:

.stack > * + * {
  margin-top: 1rem;
}

Every child that has a previous sibling gets top margin; the first child does not. Today, gap in flex and grid layouts often handles this more cleanly, but the pattern remains useful in flow content like article bodies, where you cannot easily switch the container to flex.

The subsequent-sibling combinator (~)

A ~ B matches every B that comes after an A under the same parent, whether or not other elements sit between them.

h2 ~ p {
  color: var(--text-muted);
}

Here every paragraph after the first h2 in a container is muted, including paragraphs after later headings, because they all follow that first h2.

The ~ combinator is most often paired with form state:

#show-advanced:checked ~ .advanced-options {
  display: block;
}

When the checkbox is checked, the options panel that follows it becomes visible, with no JavaScript. The pattern works because the panel is a later sibling of the input. If the markup nests the panel elsewhere, the selector cannot reach it.

Sibling combinators and form validation

Sibling selectors pair well with validation pseudo-classes. A hint message placed right after an input can respond to that input’s state:

input:user-invalid + .field-error {
  display: block;
}

The error message appears only after the user has interacted with the field and left it invalid. The available states are covered in our guide to CSS form validation pseudo-classes.

The limitation: no previous-sibling combinator

Sibling combinators only look forward. There is no combinator that says “select A if it is followed by B.” For years that meant restructuring markup or reaching for JavaScript whenever an earlier element needed to react to a later one.

The :has() pseudo-class fills that gap. Put the relationship inside it, and the subject becomes the earlier element:

/* Style a heading that is immediately followed by a paragraph */
h2:has(+ p) {
  margin-bottom: 0.25rem;
}

/* Style a label whose following input is invalid */
label:has(+ input:user-invalid) {
  color: var(--danger);
}

:has(+ p) reads as “has a next sibling that is a p.” You can also combine :has() with ~ to look further ahead. Our CSS :has() selector guide covers the selector in depth, including parent selection.

Specificity of sibling selectors

Combinators themselves add nothing to specificity. h2 + p has the same specificity as h2 p: two type selectors. What counts is the simple selectors on each side, so .card + .card carries the weight of two classes. If you need a sibling rule that is easy to override, you can wrap parts of it in :where(), which contributes zero specificity. The rules are laid out in CSS specificity explained and :is() and :where().

Sibling combinators in nested CSS

Native CSS nesting supports combinators at the start of a nested rule, which keeps sibling relationships close to the element they modify:

.card {
  padding: 1rem;

  & + & {
    margin-top: 1.5rem;
  }

  ~ .card-footer {
    border-top: 1px solid var(--border);
  }
}

& + & means “a .card immediately after a .card.” A rule that begins with a combinator, such as ~ .card-footer, is relative to the parent selector. See CSS nesting explained for how nested selectors resolve.

Common mistakes

  • Expecting siblings across parents. Sibling combinators only work within the same parent. Two cards in different grid cells wrapped in separate containers are not siblings.
  • Expecting backward matching. B ~ A will never match an A that comes before B. Use :has() for that.
  • Fragile adjacency. + breaks as soon as someone inserts a wrapper, an icon, or a hidden element between the two. If the relationship is important, ~ or a class may be more robust.
  • Overusing the checkbox hack. Checkbox-and-sibling tricks can create toggles without JavaScript, but they are often less accessible than a real button with proper state. Native elements like <details> or the popover API are usually better choices for disclosure widgets.

The takeaway

The + combinator selects the element immediately after another, and ~ selects every later sibling, both within the same parent and only in the forward direction. They are ideal for spacing rules, lead paragraphs, and form-state styling without extra classes. When an earlier element needs to react to a later one, combine them with :has().

Takina Takina · · 5 min read

CSS text-overflow Ellipsis: How to Truncate Text

CSS text-overflow: ellipsis truncates overflowing text with "…", but only with overflow and white-space set. The recipe, flexbox fixes, and accessibility.

#CSS #Web Development #Frontend
Takina Takina · · 4 min read

HTML details and summary Elements Explained

The HTML details and summary elements build native disclosure widgets and accordions with no JavaScript. How they work, how to style them, and pitfalls.

#Web Development #Frontend #CSS