【问题标题】:change color of rows in a table in HTML and CSS在 HTML 和 CSS 中更改表格中行的颜色
【发布时间】:2023-03-24 11:06:01
【问题描述】:

尝试学习 HTML 和 CSS,我有一个简单的问题。

如何为表格中的每一行赋予不同的颜色?例如第 1 行是红色,第 2 行是蓝色等。

这是我的 HTML 代码:

#table {
  font-family: Arial, Helvetica, sans-serif;
  width: 600px;
  border-collapse;
  collapse;
}
#table td,
#table th {
  font-size: 12x;
  border: 1px solid #4D365B;
  padding: 3px 7px 2px 7px;
}
#table th {
  font-size: 14px;
  text-align: left;
  padding-top: px;
  padding-bottom: 4px;
  background-color: #4D365B;
  color: #918CB5;
}
#table td {
  color: #000000;
  background-color: #979BCA;
}
<table id="table">
  <tr>
    <th>Company</th>
    <th>Contact
      <th>Country</th>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
</table>

【问题讨论】:

  • 你可以使用 :nth-child(even) 和 :nth-child(odd)

标签: html css


【解决方案1】:

如果我正确理解了您的问题并且您想为每一行赋予不同的颜色?你有几个选择...

  • 为每一行添加一个类并设置它们的样式。
  • 使用直接兄弟选择器tr + tr
  • 使用: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;}
<table id="table">
  <tr>
    <th>Company</th>
    <th>Contact
      <th>Country</th>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2
      <td>3</td>
  </tr>
</table>

【讨论】:

    【解决方案2】:

    你也可以这样试试

    #table tr{background: red;}
    #table tr:nth-child(2n+1){background: blue;}
    
    #table {
    font-family: Arial, Helvetica, sans-serif;
    width:600px;
    border-collapse;collapse;
    
    #table td, #table th {
    font-size:12x;
    border:1px solid #4D365B;
    padding: 3px 7px 2px 7px;
    
    #table th {
    font-size:14px;
    text-align:left;
    padding-top:px;
    padding-bottom:4px;
    background-color:#4D365B;
    color:#918CB5;
    
    #table td {
    color:#000000;
    background-color:#979BCA;
    }
    <table id="table">
    <tr><th> Company</th><th>Contact<th>Country</th></tr>
    <tr><td> 1</td><td>2<td> 3</td></tr>
    <tr><td> 1</td><td>2<td> 3</td></tr>
    <tr><td> 1</td><td>2<td> 3</td></tr>

    【讨论】:

      【解决方案3】:

      这可以使用pseudo selectors nth-child 轻松完成。

      #table tr:nth-child(odd){background:red}
      #table tr:nth-child(even){background:blue}
      <table id="table">
      <tr><th> Company</th><th>Contact<th>Country</th></tr>
      <tr><td> 1</td><td>2<td> 3</td></tr>
      <tr><td> 1</td><td>2<td> 3</td></tr>
      <tr><td> 1</td><td>2<td> 3</td></tr>
      </table>

      【讨论】:

      • 此外,您还可以为特定行添加一个数字#table tr:nth-child(3),以防您希望所有行都具有不同的颜色
      【解决方案4】:

      tr:nth-child(even) {
        background: #ff0101;
      }
      
       tr:nth-child(odd) {
        background: #0030ff;
      }
      <table class="ex1">
        <tbody>
          <tr>
            <th>Month
      
            </th>
            <th>'94
      
            </th>
            <th>'95
      
            </th>
            <th>'96
      
            </th>
            <th>'97
      
            </th>
            <th>'98
      
            </th>
            <th>'99
      
            </th>
            <th>'00
      
            </th>
            <th>'01
      
            </th>
            <th>'02
      
            </th>
          </tr>
          <tr>
            <td>Jan
      
            </td>
            <td>14
      
            </td>
            <td>13
      
            </td>
            <td>14
      
            </td>
            <td>13
      
            </td>
            <td>14
      
            </td>
            <td>11
      
            </td>
            <td>11
      
            </td>
            <td>11
      
            </td>
            <td>11
      
            </td>
          </tr>
          <tr>
            <td>Feb
      
            </td>
            <td>13
      
            </td>
            <td>15
      
            </td>
            <td>12
      
            </td>
            <td>15
      
            </td>
            <td>15
      
            </td>
            <td>12
      
            </td>
            <td>14
      
            </td>
            <td>13
      
            </td>
            <td>13
      
            </td>
          </tr>
          <tr>
            <td>Mar
      
            </td>
            <td>16
      
            </td>
            <td>15
      
            </td>
            <td>14
      
            </td>
            <td>17
      
            </td>
            <td>16
      
            </td>
            <td>15
      
            </td>
            <td>14
      
            </td>
            <td>15
      
            </td>
            <td>15
      
            </td>
          </tr>
          <tr>
            <td>Apr
      
            </td>
            <td>17
      
            </td>
            <td>16
      
            </td>
            <td>17
      
            </td>
            <td>17
      
            </td>
            <td>17
      
            </td>
            <td>15
      
            </td>
            <td>15
      
            </td>
            <td>16
      
            </td>
            <td>16
      
            </td>
          </tr>
          <tr>
            <td>May
      
            </td>
            <td>21
      
            </td>
            <td>20
      
            </td>
            <td>20
      
            </td>
            <td>21
      
            </td>
            <td>22
      
            </td>
            <td>20
      
            </td>
            <td>21
      
            </td>
            <td>20
      
            </td>
            <td>19
      
            </td>
          </tr>
          <tr>
            <td>Jun
      
            </td>
            <td>24
      
            </td>
            <td>23
      
            </td>
            <td>25
      
            </td>
            <td>24
      
            </td>
            <td>25
      
            </td>
            <td>23
      
            </td>
            <td>25
      
            </td>
            <td>23
      
            </td>
            <td>24
      
            </td>
          </tr>
          <tr>
            <td>Jul
      
            </td>
            <td>29
      
            </td>
            <td>28
      
            </td>
            <td>26
      
            </td>
            <td>26
      
            </td>
            <td>27
      
            </td>
            <td>26
      
            </td>
            <td>25
      
            </td>
            <td>26
      
            </td>
            <td>25
      
            </td>
          </tr>
          <tr>
            <td>Aug
      
            </td>
            <td>29
      
            </td>
            <td>28
      
            </td>
            <td>27
      
            </td>
            <td>28
      
            </td>
            <td>28
      
            </td>
            <td>27
      
            </td>
            <td>26
      
            </td>
            <td>28
      
            </td>
            <td>26
      
            </td>
          </tr>
          <tr>
            <td>Sep
      
            </td>
            <td>24
      
            </td>
            <td>23
      
            </td>
            <td>23
      
            </td>
            <td>26
      
            </td>
            <td>24
      
            </td>
            <td>24
      
            </td>
            <td>24
      
            </td>
            <td>22
      
            </td>
            <td>21
      
            </td>
          </tr>
          <tr>
            <td>Oct
      
            </td>
            <td>20
      
            </td>
            <td>22
      
            </td>
            <td>20
      
            </td>
            <td>22
      
            </td>
            <td>20
      
            </td>
            <td>19
      
            </td>
            <td>20
      
            </td>
            <td>22
      
            </td>
            <td>
      
            </td>
          </tr>
          <tr>
            <td>Nov
      
            </td>
            <td>18
      
            </td>
            <td>17
      
            </td>
            <td>16
      
            </td>
            <td>17
      
            </td>
            <td>16
      
            </td>
            <td>15
      
            </td>
            <td>14
      
            </td>
            <td>15
      
            </td>
            <td>
      
            </td>
          </tr>
          <tr>
            <td>Dec
      
            </td>
            <td>15
      
            </td>
            <td>13
      
            </td>
            <td>13
      
            </td>
            <td>14
      
            </td>
            <td>13
      
            </td>
            <td>10
      
            </td>
            <td>13
      
            </td>
            <td>11
      
            </td>
            <td>
            </td>
          </tr>
        </tbody>
      </table>

      【讨论】:

        【解决方案5】:

        您可以尝试通过 CSS 选择每一行。在你的情况下:

        table tr:first-child{background:red}    or   table tr:nth-child(1){background:red}
        
        table tr:nth-child(2){background:blue}
        
        table tr:nth-child(3){background:orange}
        
        table tr:nth-child(4){background:yellow}
        
        table tr:last-child{background:purple} or table tr:nth-child(5)
        {background:purple}
        

        【讨论】:

          【解决方案6】:

          下面的一小段代码应该改变颜色表行。

           table, td, th {
              border: 1px solid red;
          }
          
          th {
              background-color: red;
              color: black;
          }
          

          来源:http://www.snoopcode.com/css/css-tables

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-08
            • 2019-11-16
            • 2014-02-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多