Alternating Table Color Rows in CSS

These 2 techniques can be used for alternating row-colors in a table.

[View All Snippets]
Show Plain Text »
  1. <style type="text/css">
  2. /* technique 1 */
  3. tbody tr:nth-child(odd){ background-color:#ccc; }
  4. /* technique 2 */
  5. TBODY TR.odd { background-color:#78a5d1; }
  6. </style>
  7.  
  8. <table>
  9. <tbody>
  10. <tr><td>Row1</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td></tr>
  11. <tr><td>Row2</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td></tr>
  12. <tr><td>Row3</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td></tr>
  13. <tr><td>Row4</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td></tr>
  14. <tr><td>Row5</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td></tr>
  15. </tbody>
  16. </table>
  17.  
  18. <table>
  19. <tbody>
  20. <tr class="odd"><td>Row1</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td></tr>
  21. <tr><td>Row2</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td></tr>
  22. <tr class="odd"><td>Row3</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td></tr>
  23. <tr><td>Row4</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td></tr>
  24. <tr class="odd"><td>Row5</td><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</td></tr>
  25. </tbody>
  26. </table>


Result for 1st technique:

Row1Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Row2Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Row3Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Row4Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Row5Lorem ipsum dolor sit amet, consectetur adipiscing elit.


Result for 2nd technique:

Row1Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Row2Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Row3Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Row4Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Row5Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Create Your Free Account
Please remember that this information is essential to use our services correctly.
After creating the account you will be able to download all of our FREE products.
Fields marked with * are mandatory






Please send me information about updates, new products, specials and discounts from ApPHP!
We recommend that your password should be at least 6 characters long and should be different from your username/email. Please use only letters of the English alphabet to enter your name.

Your e-mail address must be valid. We use e-mail for communication purposes (order notifications, etc). Therefore, it is essential to provide a valid e-mail address to be able to use our services correctly.

All your private data is confidential. We will never sell, exchange or market it in any way. Please refer to Privacy Policy.

By clicking "Create Account", you are indicating that you have read and agree to the ApPHP Terms & Conditions.