ani_css ani_css - 2 months ago 7
CSS Question

how to pick second element starting from the last one?

enter image description here

I have two elements and they have the same class and I want to pick the second element starting from the last one.
( I hope image tells you everything)

I tried

nth-child
but my two elements have same class which is why I can't do with
nth-child
any idea ?

Answer

you can use nth-last-child(2) or nth-last-of-type(2), this will select the 2 last item.

li {
  display: inline-block
}
li:nth-last-child(2) {
  color: red
}
<ul>
  <li>test</li>
  <li>test</li>
  <li>test</li>
  <li>test</li>
  <li>test</li>
</ul>
<hr />
<ul>
  <li>test</li>
  <li>test</li>
  <li>test</li>
  <li>test</li>
  <li>test</li>
  <li>test</li>
  <li>test</li>
</ul>