css - Alternate Rows with exception > inherit background color -
i have table alternate row background-color:
tr:nth-child(even) {background: #fff} tr:nth-child(odd) {background: #f4f4f4}
the table comprised of 2 types of cells, ".main" , ".sub".
i background-color alternate every other ".main", while ".sub" rows color of previous ".main".
it great if solution css, open jquery if it's best way go.
any ideas?
<table> <tr id='1' class='main'><td></td></tr> <tr id='2' class='main'><td></td></tr> <tr id='3' class='main'><td></td></tr> <tr id='4' class='main'><td></td></tr> <tr id='5' class='main'><td></td></tr> <tr id='6' class='sub'><td></td></tr> <tr id='7' class='main'><td></td></tr> <tr id='8' class='main'><td></td></tr> <tr id='9' class='sub'><td></td></tr> <tr id='10' class='sub'><td></td></tr> <tr id='11' class='main'><td></td></tr> </table>
rows 1,3,5,8 should #f4f4f4
rows 2,4,7,11 should #fffff
and each .sub row should same color preceding .main row.
(these tables dynamically generated, placement vary)
edit: here jsfiddle of failed first attempt jquery http://jsfiddle.net/xjdzm/
i don't think possible pure css, seem need style odd rows of .main
, not odd rows and .main
, , :nth-child
can not (you can't use (tr.main):nth-child(odd)
, not mention requirement .sub
more complicate).
so here's jquery solution:
$("tr.main").filter(":even").css("background-color","#ccc");//change #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 don't use jquery, i'm not sure if there's better way code. api document find methods work.
Comments
Post a Comment