Descendant Combinator: SelectorA SelectorB
The descendant combinator (whitespace, ) selects all target descendant elements of the element matched by the preceding selector.
It uses the selectorA selectorB syntax, where whitespace ( , a space character) separates the two selectors and selects all elements matching selectorB that are descendants of an element matching selectorA.
(Descendants include children, grandchildren, great-grandchildren, and all further descendants.)
It does not matter whether there is one space ( ) or several consecutive spaces. However, a single space is recommended for readability.
Syntax
selectorA selectorB {
/* ... */
}
Example - 1
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.
Example - 2
li {
list-style-type: disc;
}
li li {
list-style-type: circle;
color: red;
}
<ul>
<li>
<div>Item li</div>
<ul>
<li>Subitem li li</li>
<li>Subitem li li</li>
</ul>
</li>
<li>
<div>Item li</div>
<ul>
<li>Subitem li li</li>
<li>Subitem li li</li>
</ul>
</li>
</ul>
-
Item li
- Subitem li li
- Subitem li li
-
Item li
- Subitem li li
- Subitem li li
Child Combinator: SelectorA > SelectorB
To select only the child elements of the preceding element, use the child combinator (>).
(Children include only direct children, not descendants such as grandchildren, great-grandchildren, and all further descendants.)
Syntax
selectorA > selectorB {
/* ... */
}
Example
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.
Browser Compatibility
| Selector |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
SelectorA SelectorB
|
1 | 12 | 1 | 1 |
Specifications
| Specification | |
|---|---|
SelectorA SelectorB
|
Selectors Level 4 #descendant-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 Child Combinator (>) – Select All Direct Child Elements
- CSS Selector List (,) – Multiple Selectors Separated by Commas