3

Im about styling a table. I want each odd row to have a specific background except the first row which contains headers.

I have used the following code which does not work:

.new-items-table tr:nth-of-type(odd):not(.new-items-table tr):nth-of-type(1)
{
background-color: red;
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105

3 Answers3

6

You can just do:

.new-items-table tr:nth-child(2n + 3) {
    background-color: red;
}

2n + 1 is the same thing as odd. 2n + 3 skips the first two rows.

Demo: http://jsfiddle.net/YVTTA/

Blender
  • 289,723
  • 53
  • 439
  • 496
4

Some options:

1. ~ selector - http://jsfiddle.net/5sKqX/

.new-items-table tr ~ tr:nth-of-type(odd) {background-color:red;}

It matched trs that are after other trs, so it skips the header row.

2. use <thead> - http://jsfiddle.net/5sKqX/1/

<thead>
    <tr><th>Header</th></tr>        
</thead>
<tbody>
    <tr><td>1</td></tr>
    <tr><td>2</td></tr>
    <tr><td>3</td></tr>
</tbody>

Css:

.new-items-table tr:nth-of-type(even) {background-color:red;}

3. use :not(:first-child) - http://jsfiddle.net/5sKqX/2/

.new-items-table tr:nth-of-type(odd):not(:first-child) {background-color:red;}
Community
  • 1
  • 1
Kobi
  • 135,331
  • 41
  • 252
  • 292
2

As I wrote in the comment to your question, wrap your headers in <thead></thead> and the body of a table in <tbody></tbody>. Then you could simply use the following rule:

.new-items-table tbody tr:nth-child(odd)
{
    background-color: red;
}
pkacprzak
  • 5,537
  • 1
  • 17
  • 37