Child Combinator: SelectorA > SelectorB
The child combinator (>) selects all target direct child elements of the element matched by the preceding selector.
It uses the selectorA > selectorB syntax, where the greater-than sign (>) separates the two selectors and selects all elements matching selectorB that are direct children of an element matching selectorA. (Only direct children are included, not descendants such as grandchildren, great-grandchildren, and all further descendants.)
Syntax
selectorA > selectorB {
/* ... */
}
Example - 1
div > p {
background-color: yellowgreen;
}
article {
border: 1px dashed red;
}
<h2>CSS Child Combinator</h2>
<p>This is a p element.</p>
<div>
<p>Selects all target direct child p elements of the preceding div element.</p>
<p>Selects all target direct child p elements of the preceding div element.</p>
<article>
<h3>article</h3>
<p>The grandchild p element is not selected.</p>
</article>
</div>
CSS Child Combinator
This is a p element.
Selects all target direct child p elements of the preceding div element.
Selects all target direct child p elements of the preceding div element.
article
The grandchild p element is not selected.
Example - 2
span {
background-color: aqua;
}
div > span {
background-color: gold;
}
<div>
<span>
This is a span element that is a direct child of div.
<span>This is a span element that is a grandchild of div.</span>
</span>
</div>
<span>This span element is unrelated to div.</span>
Whitespace before and after the greater-than sign (>) separating the selectors is optional, but recommended for visual clarity.
/* Whitespace before and after the greater-than sign (>) is optional,
but recommended for visual clarity. */
div>span { /* Not recommended */
background-color: gold;
}
div > span { /* Recommended */
background-color: gold;
}
Descendant Combinator: SelectorA SelectorB
To select all target descendant elements of the preceding element, not just its direct children, use the descendant combinator (whitespace). (Descendants include children, grandchildren, great-grandchildren, and all further descendants.)
Syntax
selectorA selectorB {
/* ... */
}
Example
div p {
background-color: yellowgreen;
}
div article {
border: 1px dashed red;
}
<h2>CSS Descendant Combinator</h2>
<p>This is a p element.</p>
<div>
<p>Selects all target descendant p elements of the preceding div element.</p>
<p>Selects all target descendant p elements of the preceding div element.</p>
<article>
<h3>article</h3>
<p>
Also selects the descendant p element nested inside article,
which is a grandchild of the preceding div element.
</p>
</article>
</div>
CSS Descendant Combinator
This is a p element.
Selects all target descendant p elements of the preceding div element.
Selects all target descendant p elements of the preceding div element.
article
Also selects the descendant p element nested inside article, which is a grandchild of the preceding div element.
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
SelectorA > SelectorB
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
SelectorA > SelectorB
|
Selectors Level 4 #child-combinators |
References
See Also
- CSS * Selector – Selects All Elements
- CSS Element Selector – Select by Element Name
- CSS Subsequent-Sibling Combinator (~) – Select All Subsequent Sibling Elements
- CSS Next-Sibling Combinator (+) – Select the Next Sibling Element
- CSS Descendant Combinator – Select All Descendant Elements
- CSS Selector List (,) – Multiple Selectors Separated by Commas