I don't think this is possible with pure CSS, as you seem to need to style the odd rows of .main
, not odd rows and .main
, and :nth-child
can not do that (you can't use (tr.main):nth-child(odd)
, not to mention your requirement with .sub
is even more complicate).
So here's a jQuery solution:
$("tr.main").filter(":even").css("background-color","#CCC");//change to #F4F4F4
$("tr.main").filter(":odd").css("background-color","#FFF");
$("tr.sub").each(function(i,e){
$(this).css("background-color",$(this).prev().css("background-color"));
});
Sorry that I don't use jQuery, so I'm not sure if there's any better way to code. I just look up the API document to find methods that work.