[英]Set alternate color for tables rows and columns in css or javascript
I have also tried using below codes. 我也尝试过使用下面的代码。 it doesn't work for this case.
在这种情况下不起作用。
table>tbody>tr>td:nth-child(odd){
background-color: #F2F2F2;
}
table>tbody>tr:nth-child(odd){
background-color: #FFF !important;
}
table>tbody>tr>td:nth-child(even){
background-color: #F7F7F7;
}
See the difference: we are using odd
and even
on tr
, not td
: 看看区别:我们在
tr
上使用odd
和even
,而不是td
:
table>tbody>tr:nth-child(odd) >td{ background-color: #eee; } table>tbody>tr:nth-child(even)>td{ background-color: #ccc; }
<table> <tbody> <tr><td>. . . . .</td><td>. . . . .</td><td>. . . . .</td></tr> <tr><td>. . . . .</td><td>. . . . .</td><td>. . . . .</td></tr> <tr><td>. . . . .</td><td>. . . . .</td><td>. . . . .</td></tr> <tr><td>. . . . .</td><td>. . . . .</td><td>. . . . .</td></tr> <tr><td>. . . . .</td><td>. . . . .</td><td>. . . . .</td></tr> </tbody> </table>
It seems this first part was answered here: 似乎在这里回答了第一部分:
Alternate table row color using CSS? 使用CSS的替代表格行颜色?
If you want the "doubled" alternate, extend the concept to all combinations: 如果要“加倍”替代,请将概念扩展到所有组合:
table>tbody>tr:nth-child(odd)> td:nth-child(odd) {background-color:#aaa} table>tbody>tr:nth-child(odd)> td:nth-child(even){background-color:#888} table>tbody>tr:nth-child(even)>td:nth-child(odd) {background-color:#eee} table>tbody>tr:nth-child(even)>td:nth-child(even){background-color:#ddd}
<table> <tbody> <tr><td>. . . . .</td><td>. . . . .</td><td>. . . . .</td></tr> <tr><td>. . . . .</td><td>. . . . .</td><td>. . . . .</td></tr> <tr><td>. . . . .</td><td>. . . . .</td><td>. . . . .</td></tr> <tr><td>. . . . .</td><td>. . . . .</td><td>. . . . .</td></tr> <tr><td>. . . . .</td><td>. . . . .</td><td>. . . . .</td></tr> </tbody> </table>
Your column css
has overridden the row css
. 您的列
css
覆盖了行css
。 Hence changed them for odd
and even
rows separately as below. 因此,如下将它们分别更改为
odd
行和even
行。
table>tbody>tr:nth-child(odd){ background-color: #F7F7F7 !important; } table>tbody>tr:nth-child(even){ background-color: #FFF !important; } table>tbody>tr:nth-child(odd)>td:nth-child(odd){ background-color: #F2F2F2 !important; } table>tbody>tr:nth-child(odd)>td:nth-child(even){ background-color: #F7F7F7; } table>tbody>tr:nth-child(even)>td:nth-child(odd){ background-color: #FFF !important; } table>tbody>tr:nth-child(even)>td:nth-child(even){ background-color: #F2F2F2; }
<table> <tbody> <tr> <td>Row 1 Column 1</td> <td>Row 1 Column 2</td> <td>Row 1 Column 3</td> </tr> <tr> <td>Row 2 Column 1</td> <td>Row 2 Column 2</td> <td>Row 2 Column 3</td> </tr> <tr> <td>Row 3 Column 1</td> <td>Row 3 Column 2</td> <td>Row 3 Column 3</td> </tr> <tr> <td>Row 4 Column 1</td> <td>Row 4 Column 2</td> <td>Row 4 Column 3</td> </tr> </tbody> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.