CSS combinatorSelectors

A + B

The + sign selects element B that sits directly after element A and shares the same parent.

On this page

Example

#
CSS
h3 + p {
  font-size: 17px;
  color: #fff;
}
Preview

Definition and usage

#

The next-sibling combinator, also called the adjacent sibling combinator, links two elements that sit right next to each other. The notation h2 + p hits the paragraph that comes right after an h2 heading, but not the paragraphs that follow it, and not a paragraph separated by another element.

Both elements must share the same parent, and B must come after A, never before it. Text and comments between them donโ€™t count, only elements do.

Typical uses include highlighting the first paragraph after a heading, spacing between adjacent elements (li + li), and reacting to a form fieldโ€™s state: input:checked + label changes the label that sits right after a checked field.

CSS has no separate previous-sibling combinator. When you want to style an element that comes before another one, use :has(): h2:has(+ p) selects a heading thatโ€™s followed by a paragraph.

Syntax

#
Syntax
A + B { property: value; }

Example selectors

#
  • .start + span

    Only the element right after .start. The 4 no longer matches.
  • span + span

    Every span thatโ€™s directly preceded by another span. The 6 drops out, because its predecessor is an em.
  • .start + span + span

    Combinators chain together: here, the second element after .start.
  • .demo > * + *

    Every child of the box that has a predecessor. Only the first element stays gray, which is why this pattern is used for spacing between elements.

Example selectors in practice

#

.start + span

Only the element right after .start. The 4 no longer matches.
.start + span { background: #7c3aed; color: #fff; }

span + span

Every span thatโ€™s directly preceded by another span. The 6 drops out, because its predecessor is an em.
span + span { background: #7c3aed; color: #fff; }

.start + span + span

Combinators chain together: here, the second element after .start.
.start + span + span { background: #7c3aed; color: #fff; }

.demo > * + *

Every child of the box that has a predecessor. Only the first element stays gray, which is why this pattern is used for spacing between elements.
.demo > * + * { background: #7c3aed; color: #fff; }

More examples

#
A label that reacts to a checked field
CSS
.option input:checked + label {
  border-color: #a78bfa;
  background: rgb(124 58 237 / 0.2);
  color: #fff;
}
Preview

Click the second option. The style moves to the label that sits right after the checked field.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • For spacing between list items, li + li { margin-top: 8px; } is enough: the first item wonโ€™t get an unnecessary margin. In flex and grid containers, gap is simpler.
  • Keep the HTML order for form fields: input first, then label. The combinator only works forward.
  • You can select the previous sibling with :has(), for example label:has(+ input:invalid) is a label that comes before an invalid field.

Related entries

#

Spotted a mistake or missing an example? Write to us.