Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 139 ยท Selectors

Combinators

Learn the four CSS combinators (descendant, child >, next-sibling + and subsequent-sibling ~) and how to select elements by their place in the document tree.

The basic selectors from the previous lesson look at one element at a time: its tag, its classes, its ID. Often, though, where an element sits matters more than what it is. A link inside the footer should look different from a link in an article. The paragraph right after a heading might need less space above it.

Combinators express those relationships. They sit between two selectors and describe how the matched elements relate in the document tree. CSS has four of them, and each is a single character (or a single space). Once you know them, you can target elements precisely without sprinkling extra classes all over your HTML.

The document as a family tree

Combinators use family words, so it helps to picture the HTML as a tree:

HTML
<article>
  <h2>Title</h2>
  <p>First paragraph with a <a href="#">link</a>.</p>
  <p>Second paragraph.</p>
</article>
  • <article> is the parent of <h2> and both <p> elements. Those three are its children.
  • The <a> is a descendant of <article> (a grandchild), but not a child of it.
  • <h2> and the two paragraphs are siblings: they share the same parent.
  • The first <p> comes immediately after <h2>; the second <p> comes later, but not immediately.
Combinator Syntax Matches B whenโ€ฆ
Descendant A B B is anywhere inside A
Child A > B B is a direct child of A
Next-sibling A + B B comes immediately after A, same parent
Subsequent-sibling A ~ B B comes anywhere after A, same parent

In every case, the rule styles B, the rightmost part. A is only a condition.

The descendant combinator (space)

A space between two selectors means "B somewhere inside A", at any depth.

CSS
footer a {
  color: #cbd5e1;
}

This styles every link inside a <footer>, whether the link is a direct child or buried five levels deep in lists and divs. It is the most common combinator, and the easiest to write by accident: .menu .item (with a space) is completely different from .menu.item (without one).

Descendants at any depth

The descendant combinator is convenient, but it can reach further than you meant. If a .sidebar ever contains a nested widget with its own links, those links get the sidebar styles too. When you only want the first level, use the child combinator.

The child combinator (>)

A > B matches B only when its parent is A. Grandchildren and deeper descendants are ignored.

CSS
.menu > li {
  display: inline-block;
}

This is ideal for nested structures such as menus, where the top-level items behave differently from items in a submenu.

Child vs descendant in a nested list

The nested "Laptops" and "Tablets" items get the text color from .menu li but not the border, because their parent is the inner <ul>, not .menu.

Spaces around >, + and ~ are optional: ul>li and ul > li mean the same thing. Most people add the spaces for readability.

The next-sibling combinator (+)

A + B matches B when it comes immediately after A and both share the same parent. Only the very next element counts; text between them doesn't matter, but another element does.

A classic use is spacing between consecutive items. Instead of adding a margin to every item and then removing it from the last one, add it only where an item follows another:

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

Another is styling the element right after a heading:

Styling what comes right after

The divider lines appear only between the list items. The first item has no previous sibling, so li + li doesn't match it.

The subsequent-sibling combinator (~)

A ~ B matches every B that comes anywhere after A under the same parent, not just the next one. (Older material calls it the "general sibling combinator".)

CSS
h2 ~ p {
  margin-left: 1rem;
}

This indents every paragraph that follows an <h2> within the same parent, even with other elements in between. Paragraphs before the heading are unaffected: sibling combinators only look forward.

A practical use is reacting to a form control's state. A checkbox can change the look of the elements that follow it:

A checkbox controls its later siblings

The :checked part is a pseudo-class; you will learn it properly soon. For real disclosure widgets, prefer the built-in <details> element, which is accessible out of the box. The checkbox trick is shown here because it demonstrates ~ so clearly.

Chaining combinators

You can chain as many combinators as you like. Read the selector from right to left: the rightmost part is what gets styled, and each step to the left adds a condition.

CSS
.article > h2 + p a { โ€ฆ }

Right to left: style an <a>... that is inside a <p>... that comes right after an <h2>... that is a direct child of .article.

Long chains like this are fragile. If someone wraps the heading in a <div>, the rule silently stops matching. A few guidelines:

  • Keep it short. One combinator is usually enough. Two is fine. Four is a warning sign.
  • Prefer a class when the relationship isn't the point. If you are writing .card > div > div > span, give that span a class.
  • Use combinators for real relationships. "Space between consecutive items" or "links inside the footer" are relationships worth expressing.

What combinators can't do

Combinators only look down the tree (descendants and children) and forward among siblings. There is no combinator for "the parent of B" or "the sibling before B". For years that was a famous gap in CSS. Today, the :has() pseudo-class fills it, for example li:has(+ li.active) matches the item just before an active one. You will meet it at the end of this chapter.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does nav a select?

    A space is the descendant combinator: it matches the right-hand element anywhere inside the left-hand one, and the rule styles the <a>.
  2. 2. Given <ul class="menu"><li>A<ul><li>B</li></ul></li></ul>, which items does .menu > li match?

    Item B's parent is the inner <ul>, not .menu, so only the top-level item A is a child of .menu.
  3. 3. Which selector adds a border between list items but not above the first one?

    li + li matches an item that directly follows another item, so the first item never matches.
  4. 4. How does h3 ~ p differ from h3 + p?

    Both look forward among siblings, but + stops at the immediately following element while ~ matches all later ones.
  5. 5. In .post > h2 + p em, which element receives the styles?

    The rightmost compound selector is always the subject; everything to its left is a condition.

Summary

  • Combinators select elements by their relationship in the document tree.
  • Descendant (A B): B anywhere inside A.
  • Child (A > B): B directly inside A.
  • Next-sibling (A + B): B immediately after A, same parent.
  • Subsequent-sibling (A ~ B): any B after A, same parent.
  • The rightmost part is what gets styled; read long selectors right to left.
  • Keep chains short, and use :has() when you need to look up the tree or backward.

Next: Attribute Selectors โ€” matching elements by their attributes and values.