7

Given an ul with a structure like this:

<ul>
    <li class="product">...</li>
    <li class="product">...</li>
    <li class="product">...</li>
    <li class="product">...</li>
    <li class="spot">...</li>
    <li class="product">...</li>
    <li class="product">...</li>
</ul>

Is there any way using CSS3 to target every other occurance of a li with the class product.

I've tried using both nth-of-child and nth-of-type in various configurations to no luck, both seem to target every other li element regardless of the class is has.

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
Hans Skov
  • 661
  • 2
  • 9
  • 19
  • 2
    Can you give an example of what you tried? – Andrew Apr 18 '12 at 15:08
  • 1
    Possible duplicate of http://stackoverflow.com/questions/5080699/using-css-even-and-odd-pseudo-classes-with-list-items – Sampson Apr 18 '12 at 15:09
  • I've tried using .product:nth-of-type(2n) and .product:nth-child(2n) but both will count the li element with class=spot in the flow and hit the li element right after the one with class=spot. – Hans Skov Apr 18 '12 at 15:22
  • Not sure if this will help, but... http://stackoverflow.com/a/10012244/206403 – gen_Eric Apr 18 '12 at 15:23
  • @Jonathan no I don't think the issue is the same – Hans Skov Apr 18 '12 at 15:23
  • @HansSkov How does it differ? Elements are either odd or even. You want to know how to select all of the evens, or all of the odds. That was the question asked elsewhere, and an answer was provided. – Sampson Apr 18 '12 at 15:28
  • 1
    @JonathanSampson: He wants all even (or odd) `.product`s, not just all even (or odd) `
  • `s.
  • – gen_Eric Apr 18 '12 at 15:30
  • 1
    @JonathanSampson I want to select every other element with a specific class – Hans Skov Apr 18 '12 at 15:30
  • @Rocket So the only difference is the presence of a class in the selector? I personally don't think that merits a new question. – Sampson Apr 18 '12 at 15:31
  • 1
    @JonathanSampson: given the difficulty we're having providing an answer, it seems to... – David Thomas Apr 18 '12 at 15:31
  • @JonathanSampson: I think it does since `.product:nth-child(odd)` doesn't work as expected. This selects elements that are class product AND also an odd child, it doesn't select the odd numbered `.product` elements. – gen_Eric Apr 18 '12 at 15:35
  • Does this answer your question? [How can I style even and odd elements?](https://stackoverflow.com/questions/5080699/how-can-i-style-even-and-odd-elements) – shreyasm-dev Sep 27 '20 at 22:36