Css specificity :last-child

Turbodurso picture Turbodurso · Nov 4, 2009 · Viewed 36.4k times · Source

I would like to use the following to target the last link (a) of the last ul inside my div. So this is what came to mind:

#menu ul:last-child li a {
       /*.....*/
}

I cant manually add a class to that element, and even if i wanted to do it dynamically i would still have to target the element the above way.

Any ideas why this is not working?

Answer

Arve Systad picture Arve Systad · Nov 4, 2009

If you want the last link in the last ul (assuming the link is inside a li-element), this is what you want:

#menu ul:last-child li:last-child a {
       /*.....*/
}
  • #menu returns the menu element.
  • ul:last-child returns the last ul within #menu
  • li:last-child returns the last li within that ul

Haven't tried it, but i guess this would work.