KC Rajput KC Rajput - 7 months ago 21
HTML Question

Alternate table row color using CSS?

I am using a table with alternate row color with this.

tr.d0 td {
background-color: #CC9999;
color: black;
tr.d1 td {
background-color: #9999CC;
color: black;

<tr class="d0">
<tr class="d1">

Here I am using class for
, but I want to use only for
. When I use class for table than this apply on

Can I write my HTML like this using CSS?

<table class="alternate_color">

How can I make the rows have "zebra stripes" using CSS?


There is a CSS selector, really a pseudo-selector, called nth-child. In pure CSS you can do the following:

tr:nth-child(even) {
    background-color: #000000;

Note: No support in IE 8.

Or, if you have jQuery:

  $("tr:even").css("background-color", "#000000");