Search code examples
javascriptcss-tables

bgcolor for alternative rows in a table dynamically using CSS


How to give two different bgcolor for alternative rows in a table dynamically using CSS.

I don't want to use jQuery for solving this problem.


Solution

  • Use :nth-child() pseudo class

    tr:nth-child(odd){
        background-color:green
    }
    tr:nth-child(even){
        background-color:yellow
    }​
    

    DEMO

    Here is few more selector example.