Conditional alternative table row styles

css, html-table

Solution

tr:nth-child(even) { background: #FFF; }
tr:nth-child(odd) { background: #EEE; }

Does not work in IE, but it's a purely presentational thing, the content will work fine anyway, so I don't think it's a huge issue -- depending on the % of regular IE users on your site.

Problem

Is it possible to style alternate table rows without defining classes on alternate `<tr>` tags? With the following table, can CSS define alternate row styles WITHOUT having to give the alternate rows the class "row1/row2"? row1 can be default, so row2 is the issue. ``` <style> .altTable td { } .altTable .row2 td { background-color: #EEE; } </style> <table class="altTable"> <thead><tr><td></td></tr></thead> <tbody> <tr><td></td></tr> <tr class="row2"><td></td></tr> <tr><td></td></tr> <tr class="row2"><td></td></tr> </tbody> </table> ```

Original source