Set alternate color for tables rows and columns in css or javascript - Stack Overflow

I have also tried using below codes. it doesn't work for this case.table>tbody>tr>td:nth-

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

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;
}
Share Improve this question edited Nov 21, 2016 at 7:45 Vikrant 5,04618 gold badges51 silver badges75 bronze badges asked Nov 21, 2016 at 7:43 Manoj prabhakar RManoj prabhakar R 11 silver badge2 bronze badges 3
  • 5 Can you add your markup – Geeky Commented Nov 21, 2016 at 7:44
  • 1 Possible duplicate of Alternate table row color using CSS? – Daniel Commented Nov 21, 2016 at 7:47
  • Post your code with code Snippet It will be more helpful to get issue with your code. – TIGER Commented Nov 21, 2016 at 7:52
Add a ment  | 

2 Answers 2

Reset to default 6

Alternating rows

See the difference: we are using odd and even on tr, not 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?


Alternating rows and columns:

If you want the "doubled" alternate, extend the concept to all binations:

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. Hence changed them for odd and even rows separately as below.

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745207459a4616659.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信