【问题标题】:Background Color not working with display contents背景颜色不适用于显示内容
【发布时间】:2021-11-10 02:01:28
【问题描述】:

我正在尝试将borderbackground-color 添加到我的表中,但没有得到应用。

我的表格的 CSS :-

table {
      width: 100%;
      display: grid;
      overflow: auto;
    }
    
      table thead,
    table tbody,
    table tr {
      display: contents;
    }

尝试应用的 CSS 不起作用

tbody tr:nth-child(odd) { 
      border: solid 1px #e2e2e2;
      background-color: #f4f4f4;
    }
    tbody tr:nth-child(even) { 
      border: solid 1px #e2e2e2;
      background-color: blue;
    }

请给我一个出路,因为我需要申请background-color。 我也试过申请。

tbody tr {
background-color: blue;
}

这也不行。

提前致谢。

【问题讨论】:

  • 能不能给我们看一下表格的html

标签: css bootstrap-4 sass react-bootstrap


【解决方案1】:

尝试选择td

table {
  width: 100%;
  display: grid;
  overflow: auto;
}
tbody tr:nth-child(odd) > td{ 
  border: solid 1px #e2e2e2;
  background-color: #f4f4f4;
}
tbody tr:nth-child(even) > td{ 
  border: solid 1px #e2e2e2;
  background-color: blue;
}
table thead,
table tbody,
table tr {
  display: contents;
 }
<table>
  <thead>
    <tr>
      <th>header</th>
      <th>header</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1st row</td>
      <td>1st row</td>
    </tr>
    <tr>
      <td>2nd row</td>
      <td>2nd row</td>
    </tr>
  </tbody>
</table>

【讨论】:

  • 我无法删除display:contents,因为它将表格的元素放在一起。如果我们使用display:grid,因为我要求表格是网格
猜你喜欢
  • 2022-01-06
  • 2021-10-19
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多