liveBook · Manning

Descendant selector

As explained in Web Design Playground, Second Edition

A descendant selector is a CSS selector that matches elements nested at any depth within a specified ancestor.

A descendant selector targets elements nested anywhere inside a specified ancestor. Write the ancestor and descendant element names with a space between them, as in div p. section 19.5 section 19.15

Keep these distinctions handy:

  • The space is a combinator: it connects the ancestor to the element being targeted. section 19.5
  • The selector reaches through every nesting level, not just the next one. section 19.5
  • Use the child selector with > when only the immediately nested level should match. section 19.5
  • Combine element, class, and descendant selectors to narrow the target, such as p.footnote a.external. section 19.8

Style only the body paragraphs

A page can leave its introductory paragraphs untouched while styling paragraphs inside a div: div p { color: #444; font-family: Georgia, serif; font-size: 1rem; }. The selector skips the heading and paragraph outside the div, then matches both paragraphs inside it. section 19.5

div p

View the compact div p selector syntax used to target paragraphs within a div. section 19.5

How Web Design Playground, Second Edition covers it

How deep it goes
Defines the space combinator and demonstrates it, then stops short of one-level targeting handled by the child selector.
Examples
Uses div p to style body paragraphs and p.footnote a.external to demonstrate more precise descendant targeting.
What it assumes
Builds on basic CSS rules and HTML nesting while distinguishing ancestors, descendants, and children.
What you can do after
Apply it via CSS to style nested elements at any depth and choose child selectors when the target must be one level down.
How it connects
Connects descendant targeting with child selectors, class selectors, CSS inheritance, the cascade, and specificity.

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 Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.