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")); }); 

http://jsfiddle.net/xjdzm/1/

sorry don't use jquery, i'm not sure if there's better way code. api document find methods work.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -