Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select first label within div using css?

Tags:

html

css

Hi i want to select first label element within div with classname form_fields.

Below is the code,

 <form>
     <div className="form_fields">
         <span>first span</span>
         <div>first div</div>
         <label>
             <span>Name</span>
         </label>
         <label>Description</label>
     </div></form>

What i have tried?

.fields label:first-child {
    margin: 20px;
}

But this doesnt apply to the first label inside div of class form_fields. How can i do it? thanks.

like image 900
stackoverflow_user Avatar asked Sep 09 '26 22:09

stackoverflow_user


2 Answers

Try with first-of-type pseudo-selector:

.form_fields label:first-of-type {
    margin: 20px;
}
like image 72
Mario Cianciolo Avatar answered Sep 11 '26 10:09

Mario Cianciolo


.form_fields label:nth-of-type(1) {
    margin: 20px;
}
like image 32
Brad Avatar answered Sep 11 '26 10:09

Brad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!