Select any class element using css3
Given ulwith 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 event lion a class product.
I tried to use both nth-of-child and nth-of-type in various configurations so that no luck, both seem to target any other li element no matter what class it has.
+3
3 answers
This cannot be done using simple CSS (in any way that I don't know about yet), however a simple JavaScript solution is pretty simple:
var lis = document.querySelectorAll('li.product');
for (var i=0,len=lis.length;i<len;i++){
if (i%2 == 0){
lis[i].className = 'oddProduct';
}
}
jQuery ( , , JavaScript), , , .
+1