Skip to the workspace
Pebellslearn by building
Combinator Selectors  ›  Lesson

The Child Combinator

child 12 minThe greater-than combinator matches an element only when its parent matches the left-hand selector, not any further ancestor.

css / The Child Combinator

Explore each scene at your own pace.

01 · The rule

The rule

The Child Combinator

The greater-than combinator matches an element only when its parent matches the left-hand selector, not any further ancestor.

Restrict a selector to immediate children and compare its result with a descendant selector.

The menu section's own heading should have a line under it, but the dish headings inside the cards should not. "Inside the menu" is too loose for that. You need "directly inside": a child, not a grandchild.

The rule

> means "directly inside": one level down, no further.

A > B matches a B whose parent is an A. Grandchildren and deeper descendants do not count.

<main>
  <section id="menu">
    <h2>Today's menu</h2>
    <article class="dish">
      <h3>Masala omelette</h3>
      <p class="price">₹180</p>
      <p>Three eggs, green chilli and coriander.</p>
    </article>
    <article class="dish special">
      <h3>Mango lassi</h3>
      <p class="price">₹120</p>
      <p>Thick, cold and only in summer.</p>
    </article>
  </section>
  <section id="visit">
    <h2>Visit us</h2>
    <p>Open 8 am to 6 pm, every day.</p>
  </section>
</main>
#menu > h2 {
  border-bottom: 2px solid navy;
}

#menu > h2 matches "Today's menu", because its parent is section#menu. Compare:

  • #menu > h3 matches nothing. The dish names are grandchildren of #menu; their parent is an article.
  • #menu h3, with a space, matches both dish names.
  • main > section matches both sections.
  • .dish > p matches the four paragraphs in the cards, whose parent is the article.

Spaces around > are optional: #menu>h2 means the same thing, though the spaced form is easier to read.

The child combinator adds nothing to specificity. #menu > h2 scores (1,0,1), exactly like #menu h2.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.