How to select all child elements recursively using CSS?
A child selector matches when an element is the child of some element. A child selector is made up of two or more selectors separated by “>”. It is also known as element > element selector. It select all element of specific parent.
Syntax:
- Select all child elements.
element > element
- If child elements select recursively then use the following syntax.
div.class, div.class > * { // CSS Property }
Example 1: This example selects all the child element.
<!DOCTYPE html> <html> <head> <title> Child element selector </title> <style> div > p { background-color: green; } </style> </head> <body> <div> <p>Paragraph 1</p> <p>Paragraph 2</p> <span> <p>Paragraph 3</p> </span> </div> <p>Paragraph 4</p> <p>Paragraph 5</p> </body> </html> |
chevron_right
filter_none
Output:

Example 2: This example selects all the child elements recursively.
<!DOCTYPE html> <html> <head> <title> Child element selector </title> <style> div.GFG, div.GFG > * { background-color: green; } </style> </head> <body> <div class="GFG"> <p>Paragraph 1</p> <p>Paragraph 2</p> <span> <p>Paragraph 3</p> </span> </div> <p>Paragraph 4</p> <p>Paragraph 5</p> </body> </html> |
chevron_right
filter_none
Output:

Recommended Posts:
- How to select all children of an element except the last child using CSS?
- How to get specific number of child elements using CSS?
- Remove all the child elements of a DOM node in JavaScript
- How to select elements by data attribute using CSS?
- How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
- CSS | :first-child Selector
- CSS | :nth-child() Selector
- How to use a not:first-child selector in CSS?
- CSS | :nth-last-child() Selector
- CSS | :last-child Selector
- CSS | :only-child Selector
- jQuery | :only-child Selector
- CSS Child vs Descendant selectors
- jQuery | :first-child Selector
- jQuery | :nth-last-child() Selector
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



