CSS Reference ยท Selectors
The next-sibling combinator (A + B)
Reference for the CSS next-sibling combinator (+): select the element immediately after another at the same level, with examples such as the lobotomized owl.
The next-sibling (adjacent sibling) combinator is written +. A + B matches a B that comes immediately after an A, and both have the same parent. Only elements count; text and comments between them are ignored.
It is useful for styling something that depends on what precedes it: spacing between items, the label after a checkbox, or an error message after an invalid input.
CSS
h2 + p { margin-top: 0; }
Try it
Syntax and arguments
CSS
A + B { } /* B directly after A */ input:invalid + .error { } .item + .item { } /* every item that follows another item */ * + * { } /* the "lobotomized owl" */
| Part | Description |
|---|---|
A |
Selector for the earlier sibling (not styled). |
+ |
The next-sibling combinator. Spaces around it are optional. |
B |
Selector for the following sibling that is styled. |
Specificity is the sum of both sides.
Usage notes
- Only the very next element.
h3 + pskips nothing: if a<div>sits between the heading and the paragraph, nothing matches. For any later sibling use the general-sibling combinator~. - Only forward. CSS siblings are selected in document order. To style something based on a following element, use
:has(), for exampleli:has(+ li.active). - Spacing pattern.
.stack > * + *adds a margin to every child except the first. Withgapon flex or grid containers you may not need it, but it still works for plain block flow. - Form patterns.
input:checked + labelstyles a label after a checkbox; keep the label immediately after the input in the HTML so the combinator keeps working. - Order is source order. CSS
order, Flexbox direction and Grid placement do not change DOM siblings, so+follows the HTML, not the visual order. - Whitespace. Whitespace-only text and comments between the elements do not break adjacency.
Browser support
Supported in every browser.