Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way I can specify the second <span> inside an element?

I have:

<label for="modalProblemLocator">
   <span>Locator</span>
   <span>xxx</span>
</label>

<label for="modalProblemLocator">
   <span>Locator</span>
</label>

If there are two <span> elements inside I would like the second to be red. Is there a way I can specify a selector for the 2nd <span> only?

What I tried was:

.inputWithLabel {
    & > label span:last-child {
        color: @alertColor;
    }
}

But this does not work if there is just one <span>, as then the first and only <span> will also be the last.

like image 622
Samantha J T Star Avatar asked Dec 10 '22 23:12

Samantha J T Star


1 Answers

Use the nth-child in css

span{
  font-size: 30px;
  display: block;  
}
label span{
  color: blue;
}
label span:nth-child(2){
  color: red;
}
<label for="modalProblemLocator">
   <span>Locator</span>
   <span>xxx</span>
</label>
<br><br><br><br>
<label for="modalProblemLocator">
   <span>Locator</span>
</label>
<br><br><br><br>
<label for="modalProblemLocator">
   <span>Locator</span>
   <span>second</span>
     <span>third</span>
     <span>yyy</span>
     <span>xxx</span>
</label>
like image 128
Ifeanyi Chukwu Avatar answered Apr 13 '23 17:04

Ifeanyi Chukwu