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.
.menu > li { display: inline-block; }
Try it
Syntax and arguments
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 > lialso matches the items of a nestedul, because each of thoselielements is a direct child of its ownul. 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>. Sotable > trnever matches in HTML; usetable > tbody > tr. - Alternatives.
:scope > liworks withquerySelectorAllin 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.