I have one or two divs on the page with a class/classes of foo. I need to select the second one only, that means if there’s only one on the page don’t select it.
I tried div.foo:not(:first) and div.foo:nth-child(2) and both don’t seem to work.
Any ideas?
There isn’t an
:nth-of-class()selector, but if bothdiv.fooelements share the same parent, you can use one of the sibling selectors depending on whether the seconddiv.foocomes immediately after the first or not:If there are potentially more than two such
divs, you may need to undo the styles in the above rule using one of these for any subsequent elements:And don’t worry about browser support, this works in IE7+.