Favorites

What does ">", "+" and "~" do in CSS

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

What are combinators?

You've probably seen these characters in CSS files: > (greater than), + (plus) and ~ (tilde). So what exactly do they do?

A selector is the part of a CSS rule that picks which elements to style, such as p for every paragraph or .card for everything with that class. These three characters are combinators: they join two selectors together and describe how the elements are related. Instead of "style every paragraph", you can say "style paragraphs that come straight after a heading".

A quick family tree

Combinators use family words to describe how elements are nested in your HTML:

<div>                      <!-- parent of the h2 and p -->
  <h2>Title</h2>           <!-- child of the div -->
  <p>
    Some <em>text</em>      <!-- the em is a descendant of the div, but not its child -->
  </p>
</div>
  • A child sits directly inside another element, one level down.
  • A descendant sits anywhere inside another element, however deeply nested.
  • Siblings share the same parent, like the h2 and p above.

In every demo below, the elements a rule selects are highlighted in green.

The space: descendant combinator

Before we look at the three symbols, there's a fourth combinator you already use all the time: a plain space. It's called the descendant combinator, and it selects elements anywhere inside another element.

div span {
  background-color: #bbf7d0;
}

Read it as "any span inside a div, at any depth". Both the direct spans and the one nested inside another span are selected:

Results
This span is a direct child of the div.This span is nested inside another span.This span is a direct child of the div.

Keep this one in mind, because the child combinator is a stricter version of it.

The > sign: child combinator

The > sign is the child combinator. It selects elements that are direct children of another element, exactly one level down. So E > F matches an F only when its parent is an E.

div > span {
  background-color: #bbf7d0;
}
<div>
  <span>This span is a direct child of the div.
    <span>This span is nested inside another span.</span>
  </span>
  <span>This span is a direct child of the div.</span>
</div>

Same HTML as before, but now the nested span is not selected, because its parent is a span, not the div:

Results
This span is a direct child of the div.This span is nested inside another span.This span is a direct child of the div.

Tip: the child combinator is handy for menus with dropdowns. .nav > li styles only the top-level menu items, leaving the items in nested dropdown lists alone.

The + sign: next-sibling combinator

The + sign is the next-sibling combinator (older articles call it the adjacent sibling combinator). It selects an element only when it comes immediately after another element, with the same parent.

h4 + p {
  background-color: #bbf7d0;
}
<h4>Next-sibling example</h4>
<p>Paragraph one comes straight after the heading.</p>
<p>Paragraph two has no effect.</p>
<p>Paragraph three has no effect.</p>

Only the first paragraph is selected, because it's the only one directly after the h4:

Results

Next-sibling example

Paragraph one comes straight after the heading.

Paragraph two has no effect.

Paragraph three has no effect.

Note the direction: h4 + p styles the p, not the heading. The element on the right of a combinator is always the one that gets styled.

The ~ sign: subsequent-sibling combinator

The ~ sign is the subsequent-sibling combinator (older articles call it the general sibling combinator). It's like +, but less strict: it selects every matching sibling that comes after the first element, not just the next one.

h4 ~ p {
  background-color: #bbf7d0;
}
<p>This paragraph comes before the heading.</p>
<h4>Subsequent-sibling example</h4>
<p>Paragraph one is selected.</p>
<p>Paragraph two is selected.</p>
<span>A span in between doesn't stop it.</span>
<p>Paragraph three is still selected.</p>
Results

This paragraph comes before the heading.

Subsequent-sibling example

Paragraph one is selected.

Paragraph two is selected.

A span in between doesn't stop it.

Paragraph three is still selected.

Notice two things:

  • The paragraph before the heading isn't selected. Sibling combinators only look forwards, down the page.
  • Other elements in between, like the span, don't break the chain. Every following p with the same parent is selected.

Key rule: both + and ~ only select siblings, elements with the same parent. A paragraph inside a different container won't be selected, even if it appears after the heading on the page.

Putting them to work

Combinators can seem abstract at first, so here are some practical patterns you can use straight away.

Even spacing between items

A popular trick combines + with the universal selector *, which matches any element. This rule adds space above every child except the first, so items are evenly spaced with no extra gap at the top:

.stack > * + * {
  margin-top: 16px;
}

Read it as "any child of .stack that comes right after another child". Developers often call * + * the "lobotomised owl" because of how it looks.

Results
First item: no space above
Second item: 16px above
Third item: 16px above

Good to know: in flexbox and grid layouts, the gap property now does this job even more simply. The owl selector is still useful for ordinary content, such as paragraphs and headings in an article.

Styling the first paragraph after a heading

Make the opening paragraph of each section a little larger, like a magazine introduction:

h2 + p {
  font-size: 1.15em;
  color: #374151;
}

Showing content when a checkbox is ticked

The ~ combinator works with the :checked pseudo-class, which matches a ticked checkbox. Together they can reveal extra content with no JavaScript:

.extra-info {
  display: none;
}
input:checked ~ .extra-info {
  display: block;
}
Results

Great! We'll include a gift receipt and leave the price off the packing slip.

Because ~ only looks forwards, the checkbox must come before the content it controls, and both must share the same parent.

Looking backwards with :has()

For years, a common question was: "How do I style an element based on what comes after it, or what's inside it?" Combinators only look down and forwards, so it couldn't be done in CSS.

The :has() pseudo-class changed that. It selects an element if it contains, or is followed by, something that matches the selector in its brackets. Many people call it the "parent selector":

/* A card that contains a ticked checkbox */
.card:has(input:checked) {
  border-color: #16a34a;
  background: #f0fdf4;
}

/* A heading that's immediately followed by a paragraph */
h4:has(+ p) {
  margin-bottom: 4px;
}

Tick an option below and watch its whole card change:

Results

Notice how :has() works together with combinators: h4:has(+ p) uses the + sign inside the brackets to style a heading based on its next sibling. :has() works in all current major browsers.

Quick reference and tips

CombinatorNameExampleSelects
(space)Descendantdiv spanAny span anywhere inside a div
>Childdiv > spanA span whose parent is a div
+Next siblingh4 + pThe p immediately after an h4
~Subsequent siblingh4 ~ pEvery p after an h4, with the same parent

Tips for using combinators

  • Read selectors from right to left. The last part is what gets styled; everything before it is the condition. .menu > li > a styles links, but only links that are direct children of list items in the menu.
  • Keep chains short. Long selectors like header nav ul li a span are hard to read and break easily when the HTML changes. Often a class on the element itself is clearer.
  • Spaces are optional around symbols. div>span and div > span mean the same thing, but the spaced version is easier to read. The space is the combinator when there's no symbol, so div span and divspan are very different!
  • Combinators don't change specificity. Specificity is how the browser decides which rule wins when two clash. Only the selectors count, so div > p and div p carry the same weight.

What changed since the old version? The original post placed its demo styles in an unscoped <style> block, including a rule that turned the paragraph after any .adjacent heading green, which could leak into the rest of the page. All demo styles in this version are scoped to the post. The demos also now use a light green highlight with dark text, which is easier to read than text on solid green.

Wrapping up

The space selects descendants at any depth, > selects direct children, + selects the very next sibling and ~ selects every sibling that follows. They can be confusing at first, but once they click, they let you target exactly the elements you want, often without adding a single extra class. And when you need to look backwards or upwards, :has() is there to help.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee