1

I have a small list with the following classes

<ul>
<li class="first"></li>
<li class="first"></li>
<li class="first"></li>
<li class="first"></li>
<li class="first--blue"></li>
<li class="first--blue"></li>
<li class="first--blue"></li>
<li class="first"></li>
<li class="first"></li>
</ul>

Now I want to change the class of the <li class="first--blue"></li> to have a blue background, but I want to change it only to the first "first--blue" not the other ones

loliki
  • 947
  • 2
  • 16
  • 38

2 Answers2

2

You can use .first--blue ~ .first--blue to indicate the first--blue that is not the first one.

Just remember to set anything that defined in .first--blue to its initial state in .first--blue ~ .first--blue.

li {
  height: 10px;
}

/* The first first--blue */
.first--blue {
  background-color: blue;
}

/* Other first--blue */
.first--blue ~ .first--blue {
  background-color: initial;
}
<ul>
  <li class="first"></li>
  <li class="first"></li>
  <li class="first"></li>
  <li class="first"></li>
  <li class="first--blue"></li>
  <li class="first--blue"></li>
  <li class="first--blue"></li>
  <li class="first"></li>
  <li class="first"></li>
</ul>
Hao Wu
  • 17,573
  • 6
  • 28
  • 60
1

You can only apply first child property to a certain tag, but not to a class. Here are detailed explaination on how to apply all class and then undo to achieve the first child class property answered previously.

Ray
  • 67
  • 1
  • 6