Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Selectors

The child combinator (A > B)

Reference for the CSS child combinator (>): select elements that are direct children of another element, with a comparison to the descendant combinator.

The child combinator is written >. A > B matches every B whose direct parent is A. Grandchildren and deeper elements are not matched, which makes it the precise counterpart of the descendant combinator.

It is the right choice when you style a component's immediate structure, such as the direct items of a list, without leaking into nested lists.

CSS
.menu > li {
  display: inline-block;
}

Try it

Only direct children

Syntax and arguments

CSS
A > B { }       /* B is a direct child of A */
A>B { }         /* spaces around > are optional */
ul > li > a { } /* chained */
.grid > * { }   /* every direct child, any type */
Part Description
A Selector for the parent.
> The child combinator. Whitespace around it is optional.
B Selector for the child that receives the styles.

Specificity is the sum of both sides; the combinator adds nothing.

Usage notes

  • One level at a time. ul > li also matches the items of a nested ul, because each of those li elements is a direct child of its own ul. To target only the outer list, anchor the chain to a class: .tree > li.
  • Layout containers. .flex > * is the standard way to style the direct items of a flex or grid container, since only direct children become flex or grid items.
  • Stricter than the space. A rule written with > is less likely to affect elements added later inside the component, which makes components easier to reason about.
  • Not about text. Text nodes are not elements and never match, but they do not affect which element is "direct".
  • Browser-created wrappers. The browser may insert elements the author did not write, such as <tbody> inside <table>. So table > tr never matches in HTML; use table > tbody > tr.
  • Alternatives. :scope > li works with querySelectorAll in JavaScript, and CSS nesting writes > li { } inside a rule.

Browser support

Supported in every browser. The relative form (> li inside a nested rule) needs nesting support.