Descendant selector in CSS - Deep Dive
Start learning this pattern below
Jump into concepts and practice - no test required
┌─────────────┐ │ Parent │ │ ┌───────┐ │ │ │ Child │ │ │ │ ┌─┐ │ │ │ │ │Grandchild│ │ │ └─┘ │ │ │ └───────┘ │ └─────────────┘ CSS selector: Parent Descendant Matches: Child, Grandchild
inside it, and that
can have inside it. This creates a tree-like structure where some elements are inside others.
elements that are inside a
is inside a
elements that are immediate children of a
elements inside a
elements inside a
elements, then checks if each has a
elements, then checks each one's ancestors to see if any is a
Selector: div p [Browser finds all <p> elements] → [For each <p>, check ancestors] ↓ [If any ancestor is <div>] → [Apply styles] ┌─────────────┐ │ <p> │ │ ↑ │ │ Ancestors: │ │ <section> │ │ <div> │ ← Match found └─────────────┘
Practice
div p do?Solution
Step 1: Understand the descendant selector syntax
The space betweendivandpmeans selectpelements insidedivelements at any depth.Step 2: Differentiate from child selector
The child selector uses>and selects only direct children, but here it's a space, so all nestedpinsidedivare selected.Final Answer:
Selects all <p> elements inside any <div> elements, no matter how deep. -> Option DQuick Check:
Descendant selector = space = nested elements [OK]
- Confusing descendant selector with child selector
- Thinking it selects only direct children
- Mixing up order of selectors
<span> elements inside <section> elements using a descendant selector?Solution
Step 1: Recall descendant selector syntax
The descendant selector uses a space between selectors to select nested elements.Step 2: Analyze each option
section > span uses child selector (>), section+span uses adjacent sibling (+), section, span selects both separately with a comma. Only section span uses a space correctly.Final Answer:
section span -> Option AQuick Check:
Space = descendant selector syntax [OK]
- Using > instead of space for descendants
- Using + which selects siblings, not descendants
- Using comma which selects separate elements
<div>
<article>
<p>Hello</p>
</article>
<p>World</p>
</div>And CSS:
div p { color: red; }Which
<p> elements will be red?Solution
Step 1: Identify the selector target
The selectordiv pselects allpelements inside anydiv, at any depth.Step 2: Check HTML structure
Bothpelements are inside thediv: one insidearticle(nested), one directly insidediv.Final Answer:
Both <p> elements inside <div> will be red -> Option AQuick Check:
Descendant selector selects all nested matches [OK]
- Thinking only direct children are selected
- Ignoring nested elements inside other tags
- Assuming only first matching element is styled
ul li a { text-decoration: none; }But the links inside list items are still underlined. What is the most likely problem?
Solution
Step 1: Understand the selector
The selectorul li acorrectly targetsainsideliinsideul.Step 2: Consider CSS specificity and overrides
If links remain underlined, likely another CSS rule with higher specificity or inline style overrides this rule.Final Answer:
There is a more specific CSS rule overriding this one. -> Option CQuick Check:
Overrides cause expected styles to fail [OK]
- Assuming descendant selector syntax is wrong
- Not checking for CSS specificity conflicts
- Ignoring inline styles or browser defaults
<em> elements inside <article> elements, but only if they are inside a <section> inside that <article>. Which CSS selector correctly targets this?Solution
Step 1: Understand the nesting order
Theemis insidesection, which is insidearticle. So the order isarticlethensectionthenem.Step 2: Analyze each selector
article section em usesarticle section emwhich matcheseminsidesectioninsidearticle. section article em reverses order incorrectly. article > section > em uses child selectors which may be too strict. article em section is invalid order.Final Answer:
article section em -> Option BQuick Check:
Descendant selector order matches nesting order [OK]
- Reversing selector order
- Using child selectors when descendants are nested deeper
- Confusing selector order with HTML structure
