CSS Descendant Selector
Predict Output Medium
Question
Which elements get the blue color?
Code
<style>
div p { color: blue; }
</style>
<div>
<p>First</p>
<span><p>Second</p></span>
</div>
<p>Third</p>
Hint
Descendant selector matches any nested element.
Explanation
The div p selector matches any <p> that is a descendant of a <div>, at any nesting level. "First" is a direct child, "Second" is nested inside a span inside the div — both match. "Third" is outside the div, so it does not match. For direct children only, use div > p (child combinator).
Related Resources
Related Lessons
- CSS Selectors
Learn CSS selectors.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.