Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

Descendant combinator

The selector `A B` matches an element `B` inside an ancestor `A`, regardless of how many levels deep it is nested.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

A B { property: value; }

In A B, whitespace between the selectors forms the descendant combinator. The matched element is B, which must have an ancestor matching A; this relationship does not select A itself.

A descendant can be an immediate child or nested deeper. In contrast, A > B requires an immediate parent matching A.

In the example, .box .item matches direct and nested, but not outside. Do not remove the space: .box.item requires both classes on one element, rather than an ancestor–descendant relationship.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Descendant combinator</title>
<style>
  .item { color: rgb(0, 0, 0); }
  .box .item { color: rgb(255, 0, 0); }
</style>
<div class="box">
  <p id="direct" class="item">direct</p>
  <section>
    <p id="nested" class="item">nested</p>
  </section>
</div>
<p id="outside" class="item">outside</p>
</html>

Result

direct: rgb(255, 0, 0)
nested: rgb(255, 0, 0)
outside: rgb(0, 0, 0)

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources