Brack
Brack

Reputation: 333

Nth-child Targeting Every Other Child But Always Including The Last

I'm trying to figure out a CSS nth-child formula to target every other child, but always including the last child.

For example:

And so on...

Is this possible with only CSS? I'm okay with using something other than or in addition to nth-child but trying to stay CSS only.

Upvotes: 0

Views: 60

Answers (1)

Łukasz Blaszyński
Łukasz Blaszyński

Reputation: 1565

Is this fine for you?

li:nth-last-child(2n+1) {
  color: red;
}
<ul>
 <li>item</li>
 <li>item</li>
 <li>item</li>
 <li>item</li>
 <li>item</li>
</ul>

<ul>
 <li>item</li>
 <li>item</li>
 <li>item</li>
 <li>item</li>
</ul>

<ul>
 <li>item</li>
 <li>item</li>
 <li>item</li>
</ul>

<ul>
 <li>item</li>
 <li>item</li>
</ul>

<ul>
 <li>item</li>
</ul>

Upvotes: 2

Related Questions