The Wayback Machine - https://web.archive.org/web/20240724100029/https://www.geeksforgeeks.org/how-to-select-all-child-elements-recursively-using-css/
Open In App

How to select all child elements recursively using CSS?

Last Updated : 29 Jan, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Using the child selector, you can select child elements. To select all child elements recursively, use the universal selector(*). 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 selects all elements of a specific parent.

Syntax:

  • Select all child elements.

parentSelector > * {
// CSS Styles
}

  • To select all the child elements of an element recursively

.parentSelector * {
// CSS Styles
}

Example 1: This example shows that how we can select elements that are direct children of GFG class.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Child element selector
    </title>
 
    <style>
        .GFG>* {
            background-color: green;
        }
    </style>
</head>
 
<body>
    <div class="GFG">
        <p>Paragraph 1</p>
        <p>Paragraph 2</p>
        <span>
            <p>Paragraph 3</p>
        </span>
        <p>Paragraph 4</p>
    </div>
    <p>Paragraph 5</p>
</body>
 
</html>


Output:

Screenshot-2024-01-15-222227

Example 2: This example shows how to selects to select all the child elements of an element recursively.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Child element selector
    </title>
 
    <style>
        .GFG * {
            background-color: green;
        }
    </style>
</head>
 
<body>
    <div class="GFG">
        <p>Paragraph 1</p>
        <p>Paragraph 2</p>
        <span>
            <p>Paragraph 3</p>
            <ul>
                <li>Item1</li>
                <li>Item2</li>
            </ul>
        </span>
    </div>
 
    <p>Paragraph 4</p>
    <p>Paragraph 5</p>
 
</body>
 
</html>


Output:

Screenshot-2024-01-15-223711

CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.



Previous Article
Next Article

Similar Reads

How to select all children of an element except the last child using CSS?
When designing and developing web applications, sometimes we need to select all the child elements of an element except the last element. To select all the children of an element except the last child, use :not and :last-child pseudo classes. Syntax: element:not(:last-child) { // CSS Style } Example 1: This example creates a navigation menu separat
2 min read
How to select "last child" with a specific class using CSS ?
In this article, we will learn to select the "last-child" with a specific class name in CSS. The last-child selector allows us to select the last element inside a set of siblings' elements within its defining element. the last-child selector is a pseudo-class that chooses the final member of the siblings in the block that contains it. Approach: The
3 min read
How to remove all the options of a select box and then add one option and select it using JQuery ?
Given a select box with a fixed number of options, the task is to remove all the options and then add one option and immediately select that particular option using jQuery. There are two approaches that can be taken to achieve this: Approach 1: Using find(), remove(), end() and append() Methods: In the following example, a select box with some opti
3 min read
How to select all elements that contains some specific CSS property using jQuery ?
Given a HTML document containing elements and some elements have specific CSS properties. The task is to select all elements having the same property value pair in them. There are two approaches that are discussed below: Approach 1: First select the elements in which we want to check for the same properties using jQuery Selector. Use filter() metho
3 min read
How to get specific number of child elements using CSS?
The :nth-child() in CSS Selector is used to select only those elements that are the nth child, regardless of type, of its parent. Syntax: : nth-child(arg) { // CSS Property; } where arg is an argument that represents the pattern for matching elements. It can be number, odd, even or linear equation. number: It represents the elements whose position
2 min read
How to count all child elements of a particular element using JavaScript ?
Given an HTML document containing some elements nested elements and the task is to count all the child elements of a particular element. It is very simple to count the number of child elements of a particular element using JavaScript. For example: If you have a parent element consisting of many child elements then you can use HTML DOM childElementC
1 min read
Select different values on two select tags in Vue.js
In this article, we will see the selection of different values on a single select and two &lt;select&gt; tags in vue.js, along with understanding their basic implementation through the examples. HTML &lt;select&gt; tag is used to create a drop-down list. Adding a list of options inside the drop-down list using the &lt;option&gt; tag. when we select
6 min read
Remove all the child elements of a DOM node in JavaScript
Child elements are the nested elements in the HTML and we have to remove them. To remove the elements in HTML we need to use DOM methods. There are so many ways to remove the child elements of a DOM node: Table of Content Using removeChild()Using innerHTML propertyUsing removeChild()In this approach, we are using the HTML DOM removeChild() method,
2 min read
How to removes all child nodes from all paragraphs in jQuery ?
In this article, we will learn how to remove all child nodes from all paragraphs in jQuery. Child nodes are the sub-tags of the paragraph. Here, our task is to remove all the sub-tags from the &lt;p&gt; tag in DOM. We can remove all child nodes from all paragraphs in jQuery by using different methods. detach() methodremove() methodempty() method Ap
4 min read
How to select elements by data attribute using CSS?
CSS allows to select HTML elements that have specific attributes or attribute values. Element can be selected in number of ways. Some examples are given below: [attribute]: It selects the element with specified attribute. [attribute="value"]: It selects the elements with a specified attribute and value. [attribute~="value"]: It selects the elements
2 min read
Article Tags :
three90RightbarBannerImg