0

Ive looked at this SO post.

I'm trying to target the last UL when there is only 2 uls, meaning if there is a 3rd ul it will not apply.

I've tried:

ul:last-child:nth-last-child(2){

HTML:

<ul>
    ....
</ul>
 <ul>
    ....
</ul>

But no luck, where am I going wrong?

Community
  • 1
  • 1
panthro
  • 22,779
  • 66
  • 183
  • 324
  • 2
    You've got them muddled up. The accepted answer to that question pairs :first-child with :nth-last-child(). You're pairing two "last child"s together. – BoltClock Sep 14 '16 at 13:39
  • @BoltClock I'm guessing we have another misleading question once again – dippas Sep 14 '16 at 13:40
  • @dippas: Yeah, it is kinda ambiguous. The asker never stated outright if there's going to be a variable number of ULs. I tend to answer conservatively, i.e. make as few assumptions as possible and try to cover cases where I can't assume. In this case, though, I'm really tempted to just dismiss it as a typo (which is why I've now closed the question as a dupe of the one they linked to). – BoltClock Sep 14 '16 at 13:42

2 Answers2

3

Use a combination of last child and second child:

ul:last-child:nth-child(2) { ... }
Christoph
  • 1,631
  • 8
  • 19
2

I'm trying to target the last UL when there is 2 ULs.

if you want to target only when are 2 ul use last-of-type or last-child

you don't ask for more than 2 uls

ul:last-of-type {
  background: red
}
<ul>
  <li>test</li>
  <li>test</li>
  <li>test</li>
</ul>
<hr />
<ul>
  <li>test</li>
  <li>test</li>
  <li>test</li>
  <li>test</li>
</ul>
dippas
  • 58,591
  • 15
  • 114
  • 126