Skip to content
Astera Technology

Limit a style to direct children

Compare the child combinator with a descendant selector on an original two-level tree.

The direct paragraph is green; the nested paragraph is purple.

Download complete case notes and answer keys ↓

Inspect the working example

The lesson section contains one direct paragraph and another inside a nested section. The direct rule uses .lesson>p; the nested paragraph has its own purple rule.

Inputs and native observations
CaseInput or inspectionResult and meaning
Child selector.lesson>pMatches direct only
Descendant selector.lesson pMatches direct and nested
Actual colorsRead both computed colorsGreen, purple
Download editable CSV ↓

Follow the browser mechanism

The child combinator requires an immediate parent-child relationship. A descendant selector can traverse more than one level. Neither selector changes the tree; it selects from the structure already present.

A broad descendant rule reaches both levels

The alternative applies one green descendant rule to both paragraphs.

Counterexample source; inspect separately
<style>body{margin:8px}</style>
<style>.lesson p{color:rgb(0,80,40)}</style><section class="lesson"><p id="direct">Direct note</p><section><p id="nested">Nested note</p></section></section>

Both paragraphs match and become green; the intended direct-only scope is lost.

Try the repair

If nested moves one level deeper, does .lesson>p begin matching it?

Show the worked answer

No. It still lacks the immediate parent relationship. The descendant selector continues to reach it.

  • Record matched ID sets before comparing colors.
  • Test the actual nesting rather than infer from indentation.

Original example files

Related resources