change color of rows in a table in HTML and CSS

后端 未结 6 644
天命终不由人
天命终不由人 2021-01-19 22:25

Trying to learn HTML and CSS and I have a simple question.

How can I give each row a different color in a table? For example row 1 is red, row 2 is blue etc.

6条回答
  •  被撕碎了的回忆
    2021-01-19 22:33

    If I understand your question correctly and you want to give every row a different color? You have a few options...

    • Add a class to each row and style those.
    • Use the direct sibling selector tr + tr
    • Use :nth-of-type

    table tr {background: red;}
    table tr:nth-of-type(2) {background: blue;}
    table tr:nth-of-type(3) {background: green;}
    table tr:nth-of-type(4) {background: yellow;}
    table tr:nth-of-type(5) {background: grey;}
    Company Contact Country
    1 2 3
    1 2 3
    1 2 3
    1 2 3

提交回复
热议问题