Bird
Raised Fist0
CSSmarkup~8 mins

Child selector in CSS - Browser Rendering Trace

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Render Flow - Child selector
[Parse CSS selector '>'] -> [Match parent elements] -> [Find direct children only] -> [Apply styles to matched children] -> [Recalculate layout] -> [Paint changes]
The browser reads the child selector '>' and applies styles only to elements that are direct children of the specified parent, then updates the layout and paints the changes.
Render Steps - 2 Steps
Code Added:HTML structure with nested elements
Before
[div]
 ├─ [p] Paragraph 1
 └─ [section]
     └─ [p] Paragraph 2
After
[div]
 ├─ [p] Paragraph 1
 └─ [section]
     └─ [p] Paragraph 2
Initial HTML structure shows a div with a direct p child and a nested p inside section.
🔧 Browser Action:Builds DOM tree with nested elements
Code Sample
Only the paragraph directly inside the div turns red and bold; the nested paragraph inside section stays default.
CSS
<div>
  <p>Paragraph 1</p>
  <section>
    <p>Paragraph 2</p>
  </section>
</div>
CSS
div > p {
  color: red;
  font-weight: bold;
}
Render Quiz - 3 Questions
Test your understanding
After applying the CSS in step 2, which paragraph is styled red and bold?
ABoth paragraphs inside div and section
BOnly the paragraph directly inside the div
COnly the paragraph inside the section
DNo paragraphs are styled
Common Confusions - 2 Topics
Why doesn't the nested paragraph inside <section> turn red?
Because the child selector '>' only styles direct children of the div. The nested paragraph is a grandchild, so it is not matched.
💡 Child selector '>' styles only one level down, not deeper.
What if I want to style all paragraphs inside the div, including nested ones?
Use the descendant selector (space) instead of '>'. It matches all paragraphs inside the div at any depth.
💡 Space selector styles all descendants; '>' styles only direct children.
Property Reference
SelectorWhat It MatchesVisual EffectCommon Use
>Direct child elements onlyStyles only immediate children, not deeper descendantsTarget direct children for precise styling
All descendantsStyles all nested elements regardless of depthGeneral descendant styling
:nth-child()Specific child by positionStyles elements based on their order among siblingsStyling specific children
+Adjacent siblingStyles the next sibling element onlyStyling immediate siblings
Concept Snapshot
Child selector '>' targets only direct children of an element. It does not style nested grandchildren or deeper descendants. Use it for precise styling of immediate child elements. Default descendant selector (space) matches all nested descendants. Example: div > p styles only paragraphs directly inside div.

Practice

(1/5)
1. What does the CSS child selector > do?
easy
A. Selects all descendants of an element
B. Selects only direct child elements of a parent
C. Selects sibling elements
D. Selects elements by their class name

Solution

  1. Step 1: Understand selector types

    The child selector > targets only elements that are direct children of a specified parent.
  2. Step 2: Compare with other selectors

    Unlike the descendant selector (space), it does not select nested grandchildren or deeper descendants.
  3. Final Answer:

    Selects only direct child elements of a parent -> Option B
  4. Quick Check:

    Child selector = direct children only [OK]
Hint: Child selector targets only immediate children, not deeper descendants [OK]
Common Mistakes:
  • Confusing child selector with descendant selector
  • Thinking it selects siblings
  • Assuming it selects all nested elements
2. Which of the following is the correct syntax to select only <li> elements that are direct children of a <ul>?
easy
A. ul > li
B. ul + li
C. ul li
D. ul ~ li

Solution

  1. Step 1: Identify the child selector syntax

    The child selector uses the > symbol between parent and child tags.
  2. Step 2: Match syntax to question

    To select <li> elements directly inside <ul>, use ul > li.
  3. Final Answer:

    ul > li -> Option A
  4. Quick Check:

    Correct child selector syntax = ul > li [OK]
Hint: Use > between parent and child tags for direct child selection [OK]
Common Mistakes:
  • Using space instead of > selects all descendants
  • Using + or ~ which select siblings, not children
  • Missing the > symbol
3. Given this HTML:
<div>
  <p>First</p>
  <section>
    <p>Second</p>
  </section>
</div>

And this CSS:
div > p { color: red; }

Which <p> elements will be red?
medium
A. Only 'Second' paragraph
B. Both 'First' and 'Second' paragraphs
C. Only 'First' paragraph
D. No paragraphs will be red

Solution

  1. Step 1: Identify direct children of <div>

    The first <p> with text 'First' is a direct child of <div>.
  2. Step 2: Check nested <p> inside <section>

    The second <p> with text 'Second' is inside <section>, which is a child of <div>, so it is a grandchild, not direct child.
  3. Final Answer:

    Only 'First' paragraph -> Option C
  4. Quick Check:

    Child selector affects direct children only [OK]
Hint: Only immediate children match > selector, nested deeper do not [OK]
Common Mistakes:
  • Assuming all nested elements are selected
  • Ignoring the nesting inside <section>
  • Confusing descendant selector with child selector
4. This CSS code is intended to color only direct <li> children of <ol> blue:
ol > li { color: blue }

But it colors all <li> elements inside nested <ol> too. What is the problem?
medium
A. CSS does not support child selectors
B. The selector should be ol li instead
C. Missing semicolon after color property
D. Nested <ol> inside <li> causes deeper <li> to be direct children of inner ol

Solution

  1. Step 1: Understand nested lists structure

    Nested <ol> inside <li> creates new direct children <li> for the inner ol.
  2. Step 2: Explain why all <li> get colored

    The selector ol > li matches direct children of any ol, including nested ones, so all nested li get colored.
  3. Final Answer:

    Nested <ol> inside <li> causes deeper <li> to be direct children of inner ol -> Option D
  4. Quick Check:

    Child selector matches direct children of each ol, including nested [OK]
Hint: Nested ol creates new direct children li for inner ol [OK]
Common Mistakes:
  • Thinking child selector ignores nested ol
  • Confusing semicolon error with selector issue
  • Believing CSS lacks child selector support
5. You want to style only the <span> elements that are direct children of <div class='container'>, but not <span> inside nested elements. Which CSS selector achieves this?
hard
A. .container > span
B. .container ~ span
C. .container + span
D. .container span

Solution

  1. Step 1: Understand the requirement

    Only <span> elements directly inside <div class='container'> should be styled, excluding nested spans.
  2. Step 2: Choose correct selector

    The child selector > selects direct children only, so .container > span matches only spans directly inside the container div.
  3. Final Answer:

    .container > span -> Option A
  4. Quick Check:

    Child selector > selects direct children only [OK]
Hint: Use > after parent class to select direct child elements only [OK]
Common Mistakes:
  • Using space selects all descendants, not just direct children
  • Using + or ~ selects siblings, not children
  • Missing the > symbol