Introduction
The descendant selector helps you style elements that are inside other elements, like decorating a room inside a house.
Jump into concepts and practice - no test required
ancestor descendant {
property: value;
}nav a {
color: blue;
}section p {
margin-bottom: 1rem;
}.container button { background-color: green; color: white; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Descendant Selector Example</title> <style> article p { color: darkred; font-weight: bold; } </style> </head> <body> <article> <p>This paragraph is inside the article and will be dark red and bold.</p> <div> <p>This paragraph is also inside the article, nested deeper, and will be styled too.</p> </div> </article> <p>This paragraph is outside the article and will have default style.</p> </body> </html>
div p do?div and p means select p elements inside div elements at any depth.> and selects only direct children, but here it's a space, so all nested p inside div are selected.<span> elements inside <section> elements using a descendant selector?<div>
<article>
<p>Hello</p>
</article>
<p>World</p>
</div>div p { color: red; }<p> elements will be red?div p selects all p elements inside any div, at any depth.p elements are inside the div: one inside article (nested), one directly inside div.ul li a { text-decoration: none; }ul li a correctly targets a inside li inside ul.<em> elements inside <article> elements, but only if they are inside a <section> inside that <article>. Which CSS selector correctly targets this?em is inside section, which is inside article. So the order is article then section then em.article section em which matches em inside section inside article. section article em reverses order incorrectly. article > section > em uses child selectors which may be too strict. article em section is invalid order.