liveBook · Manning

Descendant selector

As explained in CSS in Depth, Second Edition

A descendant selector is a CSS selector that matches an element occurring anywhere inside an ancestor matched by another selector.

A descendant selector uses a space to match an element anywhere inside a matching ancestor. For example, `.page-header .dropdown` targets a dropdown within a page header, even if other elements sit between them. section 9.1.3

That selector can solve an immediate styling problem, but it ties the dropdown’s appearance to its location. A dark dropdown needed elsewhere would require another selector or duplicated styles. Repeated context-dependent rules also raise specificity and make styles harder to find and change. A modifier class on the dropdown expresses the variation without requiring a particular ancestor. section 9.1.3

A space changes the target

Inside a `.modal` nesting block, `.is-open` produces `.modal .is-open`: an element inside the modal with the `is-open` class. To style the modal itself when it has that class, use `&.is-open`, which produces `.modal.is-open` with no space. section 8.4.1

Not every relationship needs to appear in a selector. A module subelement named `.media__image` can be styled by its class alone. Where structure is intentional, `.media__body > h4` targets an immediate child heading; unlike a space, `>` does not match headings deeper inside the body. This choice also fixes the title to an `h4`, so use a class if the heading level must vary. section 9.1.4

How CSS in Depth, Second Edition covers it

How deep it goes
Explains nesting behavior and maintenance trade-offs, but does not provide a comprehensive selector reference.
Examples
Contrasts a header dropdown selector with a modifier, and a nested modal descendant selector with a compound selector.
What it assumes
Assumes familiarity with CSS class selectors and HTML nesting while explaining what the space between selectors changes.
What you can do after
Evaluate/understand when an ancestor-dependent selector limits reuse and when a direct-child selector expresses a deliberate structural constraint.
How it connects
Connects descendant selectors to CSS nesting, selector specificity, compound selectors, child combinators, and BEM-style modifier classes.

Also explained in

Compare how 3 books explain descendant selector →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.