This isn't so much an answer as a non-answer, i.e. an example showing why one of the highly voted answers above is actually wrong.
I thought that answer looked good. In fact, it gave me what I was looking for: :nth-of-type
which, for my situation, worked. (So, thanks for that, @Bdwey.)
I initially read the comment by @BoltClock (which says that the answer is essentially wrong) and dismissed it, as I had checked my use case, and it worked. Then I realized @BoltClock had a reputation of 300,000+(!) and has a profile where he claims to be a CSS guru. Hmm, I thought, maybe I should look a little closer.
Turns out as follows: div.myclass:nth-of-type(2)
does NOT mean "the 2nd instance of div.myclass". Rather, it means "the 2nd instance of div, and it must also have the 'myclass' class". That's an important distinction when there are intervening div
s between your div.myclass
instances.
It took me some time to get my head around this. So, to help others figure it out more quickly, I've written an example which I believe demonstrates the concept more clearly than a written description: I've hijacked the h1
, h2
, h3
and h4
elements to essentially be div
s. I've put an A
class on some of them, grouped them in 3's, and then colored the 1st, 2nd and 3rd instances blue, orange and green using h?.A:nth-of-type(?)
. (But, if you're reading carefully, you should be asking "the 1st, 2nd and 3rd instances of what?"). I also interjected a dissimilar (i.e. different h
level) or similar (i.e. same h
level) un-classed element into some of the groups.
Note, in particular, the last grouping of 3. Here, an un-classed h3
element is inserted between the first and second h3.A
elements. In this case, no 2nd color (i.e. orange) appears, and the 3rd color (i.e. green) shows up on the 2nd instance of h3.A
. This shows that the n
in h3.A:nth-of-type(n)
is counting the h3
s, not the h3.A
s.
Well, hope that helps. And thanks, @BoltClock.
div {_x000D_
margin-bottom: 2em;_x000D_
border: red solid 1px;_x000D_
background-color: lightyellow;_x000D_
}_x000D_
_x000D_
h1,_x000D_
h2,_x000D_
h3,_x000D_
h4 {_x000D_
font-size: 12pt;_x000D_
margin: 5px;_x000D_
}_x000D_
_x000D_
h1.A:nth-of-type(1),_x000D_
h2.A:nth-of-type(1),_x000D_
h3.A:nth-of-type(1) {_x000D_
background-color: cyan;_x000D_
}_x000D_
_x000D_
h1.A:nth-of-type(2),_x000D_
h2.A:nth-of-type(2),_x000D_
h3.A:nth-of-type(2) {_x000D_
background-color: orange;_x000D_
}_x000D_
_x000D_
h1.A:nth-of-type(3),_x000D_
h2.A:nth-of-type(3),_x000D_
h3.A:nth-of-type(3) {_x000D_
background-color: lightgreen;_x000D_
}
_x000D_
<div>_x000D_
<h1 class="A">h1.A #1</h1>_x000D_
<h1 class="A">h1.A #2</h1>_x000D_
<h1 class="A">h1.A #3</h1>_x000D_
</div>_x000D_
_x000D_
<div>_x000D_
<h2 class="A">h2.A #1</h2>_x000D_
<h4>this intervening element is a different type, i.e. h4 not h2</h4>_x000D_
<h2 class="A">h2.A #2</h2>_x000D_
<h2 class="A">h2.A #3</h2>_x000D_
</div>_x000D_
_x000D_
<div>_x000D_
<h3 class="A">h3.A #1</h3>_x000D_
<h3>this intervening element is the same type, i.e. h3, but has no class</h3>_x000D_
<h3 class="A">h3.A #2</h3>_x000D_
<h3 class="A">h3.A #3</h3>_x000D_
</div>
_x000D_