CSS Child Combinator (>) - Direct Child Selection
Learn the CSS child combinator (>) for targeting direct children only. Compare with the descendant combinator, understand specificity, and see real-world use cases for navigation menus and layouts.
Detailed Explanation
The Child Combinator (>)
The child combinator selects elements that are direct children of a specified parent. It does not match deeper descendants.
Syntax
parent > child {
/* styles */
}
Descendant vs Child
<ul class="nav">
<li>Item 1
<ul>
<li>Nested item</li>
</ul>
</li>
<li>Item 2</li>
</ul>
/* Descendant — matches ALL li elements inside .nav */
.nav li { color: blue; }
/* Child — matches ONLY direct li children of .nav */
.nav > li { color: red; }
The nested <li> inside the inner <ul> is selected by .nav li (descendant) but NOT by .nav > li (child combinator).
Specificity
The child combinator itself has no specificity weight. The specificity comes from the selectors on either side:
/* Specificity: (0, 1, 1) — one class + one element */
.nav > li { }
Common Use Cases
- Navigation menus — Style only top-level menu items without affecting dropdowns
- Grid/Flex layouts — Apply layout properties only to direct children:
.grid > * - Component scoping — Prevent styles from leaking into nested components
- List styling — Target only the first level of a nested list
Performance
The child combinator is slightly more performant than the descendant combinator because the browser only needs to check one level of nesting rather than traversing the entire subtree.
Use Case
The child combinator is essential for component-based CSS where you need to style direct children without affecting nested elements. Navigation menus, card grids, and flexbox/grid layouts commonly use the child combinator to apply spacing, sizing, or visual properties only to immediate children.