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;
}
<table>
<tr class="d0">
<td>One</td>
<td>one</td>
</tr>
<tr class="d1">
<td>Two</td>
<td>two</td>
</tr>
</table>
Here I am using class for tr
, but I want to use only for table
. When I use class for table than this apply on tr
alternative.
Can I write my HTML like this using CSS?
<table class="alternate_color">
<tr><td>One</td><td>one</td></tr>
<tr><td>Two</td><td>two</td></tr>
</table>
How can I make the rows have "zebra stripes" using CSS?
Yes you can but then you will have to use the
:nth-child()
pseudo selector (which has limited support though):We can use odd and even CSS rules and jQuery method for alternate row colors
Using CSS
Using jQuery
Most of the above codes won't work with IE version. The solution that works for IE+ other browsers is this.
Just add the following to your html code (withing the
<head>
) and you are done.HTML:
Easier and faster than jQuery examples.
You can use nth-child(odd/even) selectors however not all browsers (ie 6-8, ff v3.0) support these rules hence why most solutions fall back to some form of javascript/jquery solution to add the classes to the rows for these non compliant browsers to get the tiger stripe effect.