Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Selectors

The descendant combinator (A B)

Reference for the CSS descendant combinator, a space between two selectors: matches elements nested at any depth, with specificity and performance notes.

A descendant combinator is a whitespace character between two selectors. A B matches every B that has an A somewhere above it in the tree: a child, a grandchild, or deeper. It is the most used combinator, because it lets you scope rules to a part of the page.

The combinator itself adds no specificity. The specificity of A B is simply the sum of the two sides.

CSS
.article p {
  line-height: 1.7;
}

Try it

Any depth

Syntax and arguments

CSS
A B { }          /* B anywhere inside A */
nav ul li a { }  /* chained: a in li in ul in nav */
.a   .b { }      /* any amount of whitespace is one combinator */
A /* x */ B { }  /* comments may separate them too */
Part Description
A Any selector that matches an ancestor element.
whitespace Spaces, tabs or line breaks. It separates the two compound selectors.
B The selector for the element that is actually styled (the subject).

Only B receives the declarations. Specificity is the sum of both parts, for example .card p is 0,1,1.

Usage notes

  • Depth is unlimited. If you want direct children only, use the child combinator A > B.
  • No space matters. .a.b (no space) means one element with both classes; .a .b means .b inside .a. This is the most common typo.
  • Leading space on pseudo-classes. .card :hover is .card *:hover (any hovered descendant), while .card:hover is the card itself. The same goes for ::before and :first-child.
  • Over-nesting hurts. Long chains like body div.main ul li a have high specificity, are tied to the HTML structure and are hard to override. Prefer short selectors with a class on the root of the component.
  • Matching is right to left. Browsers evaluate .nav a by finding each a and checking its ancestors. That is fast; the rightmost part is what decides the cost, so avoid a universal * or div on the right in huge documents.
  • Scoping tools. CSS nesting writes the same relationship as .card { p { } }.
  • Inherited versus descendant. A descendant selector sets styles for the child element directly; inheritance only passes down specific properties.

Browser support

Supported in every browser.