Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS child selector depth

Tags:

css

I have the following HTML code :

<div class="class1">
   <input type="text">
   <input type="text">
   <div class="class2">
       <input type="text">
       <input type="text">
   </div>
</div>

I want all the input fields to have a margin-left : 10px;

I know I can do the following :

.class1 > input {margin-left:10px}
.class1 > .class2 > input {margin-left:10px}

But I was wondering if I could do this with one line of CSS

.class1 >>> input {margin-left:10px}
//>>> meaning "has .class1 in his familly tree"
like image 603
IggY Avatar asked Oct 05 '15 09:10

IggY


2 Answers

You can just remove the direct descendant:

.class1 input {
    margin-left:10px
}

Alternatively if you wish to keep the direct descendant then you can have multiple selector rules:

.class1 > input,
.class1 > .class2 > input {
    margin-left:10px
}
like image 169
Curtis Avatar answered Sep 21 '22 14:09

Curtis


Here is the way of applying CSS to child elements (for example)-

.class1 input {
	margin-left:15px;
}

.class1 > input {
    background-color:lightgreen;
}

.class1 > .class2 > input {
    background-color:lightblue;
}
<div class="class1">
   <input type="text">
   <input type="text">
   <div class="class2">
       <input type="text">
       <input type="text">
   </div>
</div>
like image 40
Xahed Kamal Avatar answered Sep 18 '22 14:09

Xahed Kamal