Last Updated on February 6, 2021 by Roshan Parihar
In this tutorial, learn how to highlight alternate table row color using CSS. The Short answer is: Use nth-child
selector of CSS to give a background color to alternate table rows.
You can apply background, borders, or any other CSS to alternate table rows using the method given here. Give CSS to odd rows of a table or even rows of a table or both using CSS.
Highlight Alternate Table Row Odd
To highlight odd alternate rows of a table, you can use the keyword ‘odd’ in the nth-child
selector. To apply CSS for the odd rows of a table, use the below syntax.
1 2 3 |
table tr:nth-child(odd){ //Enter CSS here. } |
You can also use the number expression to apply the CSS to the odd alternate table rows of a table. To apply the odd alternate table rows, you have to use the number expression 2n+1
. This applies the CSS to the odd rows of the table from the start.
Below is the simple example which uses the same number expression to apply and highlight alternate table row in odd numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<style> table.odd{ border-collapse:collapse; } table.odd tr td, th{ border:1px solid #ccc; padding:5px; } table.odd tr:nth-child(2n+1){ background: #f1f1f1; } </style> <table class="odd"> <tr> <th>Sr. No.</th> <th>Name</th> <th>Country</th> <th>Highest Score</th> </tr> <tr> <td>1</td> <td>Mahendra Singh Dhoni</td> <td>India</td> <td>183*</td> </tr> <tr> <td>2</td> <td>Chris Gayle</td> <td>West Indies</td> <td>215</td> </tr> <tr> <td>3</td> <td>Rohit Sharma</td> <td>India</td> <td>264</td> </tr> <tr> <td>4</td> <td>Fakhar Zaman</td> <td>Pakistan</td> <td>210*</td> </tr> </table> |
Output
Sr. No. | Name | Country | Highest Score |
---|---|---|---|
1 | Mahendra Singh Dhoni | India | 183* |
2 | Chris Gayle | West Indies | 215 |
3 | Rohit Sharma | India | 264 |
4 | Fakhar Zaman | Pakistan | 210* |
The above example applies the background color to the odd rows of a table. Apply any CSS to the alternate rows of a table using the above-given method.
Alternate Even Row of A Table Highlighting
If you want to apply the CSS to the even rows of a table, you can use the keyword ‘even’ in the nth-child
selector. Use the below-given method to apply the CSS to even alternate rows of a table.
1 2 3 |
table tr:nth-child(even){ //Enter CSS here. } |
In addition to this, you can also apply CSS to even rows of a table using the keyword expression also. To apply CSS to even rows, use the number expression 2n
in the nth-child
selector as given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<style> table.even{ border-collapse:collapse; } table.even tr td, th{ border:1px solid #ccc; padding:5px; } table.even tr:nth-child(2n){ background: #f1f1f1; } </style> <table class="even"> <tr> <th>Sr. No.</th> <th>Name</th> <th>Country</th> <th>Highest Score</th> </tr> <tr> <td>1</td> <td>Mahendra Singh Dhoni</td> <td>India</td> <td>183*</td> </tr> <tr> <td>2</td> <td>Chris Gayle</td> <td>West Indies</td> <td>215</td> </tr> <tr> <td>3</td> <td>Rohit Sharma</td> <td>India</td> <td>264</td> </tr> <tr> <td>4</td> <td>Fakhar Zaman</td> <td>Pakistan</td> <td>210*</td> </tr> </table> |
Output
Sr. No. | Name | Country | Highest Score |
---|---|---|---|
1 | Mahendra Singh Dhoni | India | 183* |
2 | Chris Gayle | West Indies | 215 |
3 | Rohit Sharma | India | 264 |
4 | Fakhar Zaman | Pakistan | 210* |
The above example contains the table with highlighted alternate even rows.
Both Even and Odd Row Highlight
If you want to background color to alternate table rows and highlight both even and odd row together, you have to use the example as given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<style> table.evenodd{ border-collapse:collapse; } table.evenodd tr td, th{ border:1px solid #ccc; padding:5px; } table.evenodd tr:nth-child(2n+1){ background: #ffc4c4; } table.evenodd tr:nth-child(2n){ background: #b2f8ff; } </style> <table class="evenodd"> <tr> <th>Sr. No.</th> <th>Name</th> <th>Country</th> <th>Highest Score</th> </tr> <tr> <td>1</td> <td>Mahendra Singh Dhoni</td> <td>India</td> <td>183*</td> </tr> <tr> <td>2</td> <td>Chris Gayle</td> <td>West Indies</td> <td>215</td> </tr> <tr> <td>3</td> <td>Rohit Sharma</td> <td>India</td> <td>264</td> </tr> <tr> <td>4</td> <td>Fakhar Zaman</td> <td>Pakistan</td> <td>210*</td> </tr> </table> |
Output
Sr. No. | Name | Country | Highest Score |
---|---|---|---|
1 | Mahendra Singh Dhoni | India | 183* |
2 | Chris Gayle | West Indies | 215 |
3 | Rohit Sharma | India | 264 |
4 | Fakhar Zaman | Pakistan | 210* |
The above example applies different background colors to even and odd rows of a table as you find above.
You may also like to read.
- How to change background transparency of div element.
- Center align the div element horizontally using CSS.
- CSS remove dotted outline around links.
- Write Comments in CSS Codes.
- how to define CSS for Internet Explorer and add IE-only stylesheet
I hope you like this tutorial on how to highlight alternate table row colors using CSS.