Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all children in css?

Lets say I have a wrapper for some big UI element with class="wrapper".

The HTML would be like:

<div class="wrapper">
   / a lot of other elements
</div>

Now I need to select each of elements and I go like this:

.wrapper > .first_element_class{}
.wrapper > .second_element_class{}
...

Is there a way to select all wrappers children with one line? Something like this:

.wrapper{
   .first_class{}
   ...
}
like image 631
Kārlis Janisels Avatar asked Mar 27 '17 15:03

Kārlis Janisels


People also ask

How do you select multiple children in CSS?

The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.

How do I select all children except one CSS?

Use :not() with :last-child inside to select all children except last one.

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").


2 Answers

you can use the universal selector * if you don't know which element/class you will have

Something like this:

.wrapper>* {
  color: red
}
<div class="wrapper">
  <div class="test">this is red</div>
  <span>this is red</span>
  <section>this is red</section>
</div>
like image 81
dippas Avatar answered Oct 13 '22 13:10

dippas


You can use white space to match all descendants of an element.

.wrapper * {
  color: black;
}

https://www.w3.org/TR/CSS21/selector.html#descendant-selectors

like image 20
Seth Avatar answered Oct 13 '22 11:10

Seth