Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all child elements recursively in CSS

How can you select all child elements recursively?

div.dropdown, div.dropdown > * {     color: red; } 

This class only throws a class on the defined className and all immediate children. How can you, in a simple way, pick all childNodes like this:

div.dropdown,  div.dropdown > *,  div.dropdown > * > *,  div.dropdown > * > * > *,  div.dropdown > * > * > * > * {     color: red; } 
like image 846
clarkk Avatar asked Feb 05 '11 22:02

clarkk


People also ask

How do you select all elements in CSS?

The * selector selects all elements. The * selector can also select all elements inside another element (See "More Examples").

How do I access children in CSS?

The element > element selector selects those elements which are the children of the specific parent. The operand on the left side of > is the parent and the operand on the right is the children element.

How do you select all child elements in CSS except last?

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.

How do you select all child elements in CSS except first?

This selector is used to select every element which is not the first-child of its parent element. It is represented as an argument in the form of :not(first-child) element.


1 Answers

Use a white space to match all descendants of an element:

div.dropdown * {     color: red; } 

x y matches every element y that is inside x, however deeply nested it may be - children, grandchildren and so on.

The asterisk * matches any element.

Official Specification: CSS 2.1: Chapter 5.5: Descendant Selectors

like image 114
anroesti Avatar answered Sep 19 '22 17:09

anroesti