Descendant Selectors
By the end of this lesson you will be able to style an element differently depending on where it sits in the page, such as links in the menu versus links in an article. You will read an HTML page as a family tree, use the space between two selectors on purpose, and recognise "over-qualified" selectors that make CSS hard to maintain.
This is the first of three lessons on combinators, the symbols that join selectors by their relationship. Child Selectors and Adjacent & General Sibling Selectors come next.
The page is a family tree
When the browser reads your HTML, it builds a tree of elements called the DOM (Document Object Model). Every element sits inside another one, just like people in a family tree. Here is a small news page:
<body>
<header>
<nav>
<a href="/">Home</a>
</nav>
</header>
<article>
<h2>Rain delays the Pune marathon</h2>
<p>Runners waited for an hour. <a href="/weather">See the forecast</a>.</p>
</article>
</body>And here is the same page drawn as a tree:
body
├── header
│ └── nav
│ └── a "Home"
└── article
├── h2
└── p
└── a "See the forecast"We describe elements in this tree with family words. These words are used in all three combinator lessons, so learn them now:
| Word | Meaning | Example from the tree |
|---|---|---|
| parent | the element directly above | nav is the parent of the "Home" link |
| child | an element directly below | h2 and p are children of article |
| ancestor | any element above, at any distance | header, nav and body are ancestors of "Home" |
| descendant | any element below, at any distance | both a elements are descendants of body |
| sibling | elements with the same parent | h2 and p are siblings |
Think of a grandmother in Jaipur. Her children, grandchildren and great-grandchildren are all her descendants. Only her own sons and daughters are her children.
The descendant combinator: a space
A combinator joins two selectors and says how the elements must be related. The simplest one is a plain space, called the descendant combinator.
article a {
color: #b23a48;
}Read it from right to left: "select every a that has an article somewhere above it". In the tree, only "See the forecast" matches. The "Home" link has header, nav and body above it, but no article, so it is left alone.
The depth does not matter. The link is a grandchild of article here (article, then p, then a), and it would still match if it were ten levels deeper.
You can chain more than two parts. Each space means "somewhere inside":
/* links inside a list inside the sidebar */
.sidebar ul a {
text-decoration: none;
}Live coding
Step 1: the same element, styled by its location
The "Pune Times" page has links in three places: the menu, the story and the footer. Each place needs a different look. We do not add any classes to the links themselves. The location does the work.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Descendant selectors: links by location</title>
<style>
body { font-family: system-ui, sans-serif; margin: 0; }
header { background: #0d2c54; padding: 0.8rem 1rem; }
article { padding: 1rem; max-width: 38rem; }
footer { background: #eceff1; padding: 0.6rem 1rem; font-size: 0.85rem; }
/* menu links: white, no underline */
nav a { color: #fff; text-decoration: none; margin-right: 1rem; font-weight: 600; }
/* links inside the story: red and underlined */
article a { color: #b23a48; }
/* footer links: grey */
footer a { color: #546e7a; }
</style>
</head>
<body>
<header>
<nav>
<a href="#">Home</a>
<a href="#">City</a>
<a href="#">Sports</a>
</nav>
</header>
<article>
<h2>Rain delays the Pune marathon</h2>
<p>More than 4,000 runners waited at the start line for an hour.
Organisers said the route near the river was flooded.
<a href="#">See the full forecast</a>.</p>
</article>
<footer>
<a href="#">About us</a> · <a href="#">Contact</a>
</footer>
</body>
</html>You should see white menu links on a dark blue bar, a red link inside the story and small grey links in the footer. All six are plain <a> elements in the HTML.
Why not just set the colour on the parent?
You might try article { color: #b23a48; } and expect the link to turn red through inheritance (a child taking a value from its parent). It does not work for links. The browser's own style sheet already sets a colour directly on every link, and a value set directly on an element always beats an inherited one. To change link colours, you must target the a itself, and a descendant selector is the natural way to do it. CSS Inheritance explains this rule fully.
Step 2: depth does not matter
Here is a cricket scorecard. In the HTML, each <td> is a child of <tr>, which is inside <table>, which is inside a <section>. One short selector reaches the cells anyway.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Descendant selectors: depth</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1rem; }
table { border-collapse: collapse; }
/* every cell anywhere inside .scorecard */
.scorecard td,
.scorecard th { border: 1px solid #b0bec5; padding: 0.3rem 0.7rem; }
.scorecard th { background: #1b5e20; color: #fff; }
/* strong text anywhere inside the scorecard */
.scorecard strong { color: #1b5e20; }
/* the same table outside .scorecard is not touched */
</style>
</head>
<body>
<section class="scorecard">
<h2>Kochi Tuskers vs Jaipur Royals</h2>
<table>
<tr><th>Batter</th><th>Runs</th><th>Balls</th></tr>
<tr><td><strong>Meera Nair</strong></td><td>78</td><td>52</td></tr>
<tr><td>Aarav Singh</td><td>34</td><td>29</td></tr>
</table>
</section>
<h3>Practice nets timetable</h3>
<table>
<tr><th>Day</th><th>Time</th></tr>
<tr><td>Saturday</td><td>7 am</td></tr>
</table>
</body>
</html>The scorecard table gets borders and a green header row. The timetable below has the same elements but sits outside .scorecard, so it stays plain. That is the real power of descendant selectors: they let one area of the page have its own look without touching the rest.
Step 3: watch an over-qualified selector break
An over-qualified selector describes more of the HTML structure than it needs to. It works today, but it breaks as soon as the HTML changes. In this example, a developer changed the fee box from a <div> to a <section>. Run it and see which rule still works.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Over-qualified selectors</title>
<style>
body { font-family: system-ui, sans-serif; padding: 1rem; }
/* over-qualified: names body, div and ul, but the box is now a section */
body div.fees ul li span { color: #c62828; font-weight: bold; }
/* simple: one class on the wrapper, one element */
.fees-new .amount { color: #2e7d32; font-weight: bold; }
</style>
</head>
<body>
<section class="fees">
<ul>
<li>Tuition: <span>₹42,000</span></li>
<li>Hostel: <span>₹30,000</span></li>
</ul>
</section>
<section class="fees-new">
<ul>
<li>Tuition: <span class="amount">₹42,000</span></li>
<li>Hostel: <span class="amount">₹30,000</span></li>
</ul>
</section>
</body>
</html>The first list stays plain black. The long selector demands a div, and there is no longer one. The second list is green, because .fees-new .amount does not care which elements sit in between.
The trouble with over-qualified selectors
Long descendant chains cause three problems:
- They break easily. Every element you name is a promise that the HTML will never change. Change a
ulto anol, or wrap the list in a newdiv, and the style silently disappears. - They are too strong. Each part adds specificity (the strength score from CSS Specificity).
body div.fees ul li spanscores higher than a simple.amount, so overriding it later needs an even longer selector. - They are hard to read. A teammate has to trace five levels of HTML to know what is being styled.
A good rule of thumb is to keep most selectors to two or three parts, and start with a class rather than body or html:
/* over-qualified */
html body div#main ul.menu li a { color: #0d2c54; }
/* better: a class on the container, then the element */
.menu a { color: #0d2c54; }Adding the element name to a class, such as ul.menu, is another kind of over-qualifying. It adds strength and stops the class from working on an <ol>. Write .menu unless you truly need the element.
Descendant selectors reach everything below
A descendant selector matches at every depth, including inside nested boxes. .notice p styles the paragraphs of the notice, but also a paragraph inside a quote box placed inside the notice. Usually that is what you want. When you need only the first level, use the child combinator > from the next lesson, Child Selectors.
Common mistakes
- An accidental space.
p .intromeans "something with classintroinside a paragraph".p.intromeans "a paragraph with classintro". One space changes everything. - Reading left to right.
nav astyles the links, not thenav. The element that gets styled is always the last part. - Copying the whole HTML path into the selector. Name a class on the container and the element you want. Skip everything in between.
- Expecting a parent's colour to reach links. Links have their own colour from the browser, so target
adirectly.
Interview questions
1. What does the descendant combinator do?
It is a space between two selectors, as in article a. It matches elements selected by the right-hand part that have an ancestor matching the left-hand part, at any depth. The element that receives the styles is always the right-most one.
2. What is the difference between a descendant and a child?
A child is directly inside its parent, one level down. A descendant is any element inside, at any depth: children, grandchildren and so on. Every child is a descendant, but not every descendant is a child.
3. What is an over-qualified selector and why is it a problem?
It is a selector that names more structure than needed, such as body div.wrapper ul li a or ul.menu. It breaks when the HTML changes, it has high specificity that is hard to override, and it is hard to read. A short selector based on a class is more robust.
4. Does a combinator add to specificity?
No. Combinators such as the space, >, + and ~ add nothing. Only the selectors they join count, so article a scores (0, 0, 2) and .news a scores (0, 1, 1).
5. In which direction do browsers match selectors, and why does it matter?
Browsers match from right to left. For .sidebar a, they start from each <a> and look up the tree for a .sidebar ancestor. This is efficient because most elements fail the right-most check quickly, and it is one more reason to keep the right-most part specific and the chain short.
Key points
- The browser turns HTML into a tree (the DOM) of parents, children, ancestors, descendants and siblings.
- A space between selectors is the descendant combinator:
article amatches anyainside anarticle, at any depth. - The right-most part of the selector is the element that gets styled.
- Descendant selectors let each area of a page have its own look without extra classes on every element.
- Over-qualified selectors are fragile, too specific and hard to read; prefer a class plus one element.
- Combinators add nothing to specificity.
Exercise
Make a page for a Kochi college fest with three areas: a <header> with three menu links, a <main> with a schedule list containing some <strong> event names and one link, and an <aside class="sponsors"> with two links. Using only descendant selectors (no classes on the links or <strong> elements), give header links white text on a dark background, main links an orange colour, sponsor links a small grey style, and <strong> text inside <main> a dark green colour. Then draw the DOM tree of your page as a text diagram in a comment at the top of your CSS.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Dhwani Fest 2026</title>
<style>
/*
body
├── header
│ └── nav
│ └── a, a, a
├── main
│ ├── h1
│ └── ul
│ ├── li
│ │ └── strong
│ └── li
│ ├── strong
│ └── a
└── aside.sponsors
└── a, a
*/
body { font-family: system-ui, sans-serif; margin: 0; }
header { background: #263238; padding: 0.8rem; }
header a { color: #fff; margin-right: 1rem; text-decoration: none; }
main { padding: 1rem; }
main a { color: #e65100; }
main strong { color: #1b5e20; }
.sponsors { padding: 1rem; }
.sponsors a { color: #78909c; font-size: 0.85rem; margin-right: 0.8rem; }
</style>
</head>
<body>
<header>
<nav><a href="#">Home</a><a href="#">Events</a><a href="#">Register</a></nav>
</header>
<main>
<h1>Dhwani Fest 2026</h1>
<ul>
<li><strong>Battle of Bands</strong>, Friday 6 pm</li>
<li><strong>Quiz</strong>, Saturday 11 am, <a href="#">rules</a></li>
</ul>
</main>
<aside class="sponsors">
<a href="#">Kochi Books</a><a href="#">Malabar Tea House</a>
</aside>
</body>
</html>Each rule names one area and one element, so the selectors stay short. The links are styled by where they sit, and none of them needs a class.
Quick Quiz
1. In footer a, which elements receive the styles?
The right-most part is styled, and the space means "at any depth inside".
2. A <span> sits inside an <li>, inside a <ul>, inside <div class="box">. Does .box span match it?
The descendant combinator does not care how many levels are in between.
3. Why is body div.card ul li a a poor selector?
Over-qualified selectors depend on exact structure and carry extra specificity.
4. What is the specificity of .menu a?
One class and one element; the space combinator adds nothing.
Learn CSS with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
