html - 带有 CSS 的表格的棋盘样式。如何?

标签 html css css-selectors

我有下表,我想让它看起来像一个棋盘。我该怎么做?我尝试了 nth-child,但无法解决。 感谢您的帮助!

<table class="chess">
        <tr>
            <td> </td>
            <td>Brasov</td>
            <td>Zarnesti</td>       
        </tr>

        <tr>
            <td> R14701 </td>
            <td> 06:06 </td>
            <td> 06:57 </td>
        </tr>

        <tr>
            <td> R14705 </td>
            <td> 08:10 </td>
            <td> 08:59 </td>
        </tr>

        <tr>
            <td> R14709 </td>
            <td> 12:10 </td>
            <td> 12:59 </td>
        </tr>

        <tr>
            <td> R14713 </td>
            <td> 14:10 </td>
            <td> 14:59 </td>
        </tr>
    </table>

我试过了,但有些地方不对,

td:nth-child(even) {
    background: grey;
}

td:nth-child(odd){
    background: white;
}

tr:nth-child(even){
    background:white;
}

tr:nth-child(odd){
        background: grey;
}

最佳答案

您需要交替行和单元格。尝试:

tr:nth-child(even) td:nth-child(even), tr:nth-child(odd) td:nth-child(odd) {
  background: grey;
}
tr:nth-child(odd) td:nth-child(even), tr:nth-child(even) td:nth-child(odd) {
  background: white;
}

tr:nth-child(even) td:nth-child(even), tr:nth-child(odd) td:nth-child(odd) {
  background: grey;
}
tr:nth-child(odd) td:nth-child(even), tr:nth-child(even) td:nth-child(odd) {
  background: white;
}
<table class="chess">
  <tr>
    <td> </td>
    <td>Brasov</td>
    <td>Zarnesti</td>
  </tr>

  <tr>
    <td> R14701 </td>
    <td> 06:06 </td>
    <td> 06:57 </td>
  </tr>

  <tr>
    <td> R14705 </td>
    <td> 08:10 </td>
    <td> 08:59 </td>
  </tr>

  <tr>
    <td> R14709 </td>
    <td> 12:10 </td>
    <td> 12:59 </td>
  </tr>

  <tr>
    <td> R14713 </td>
    <td> 14:10 </td>
    <td> 14:59 </td>
  </tr>
</table>

关于html - 带有 CSS 的表格的棋盘样式。如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35802488/

相关文章:

html - Chrome 正在覆盖我的 CSS 中指定的字体大小

python - 从存储在数据库中的 HTML 渲染 iframe

html - CSS 在 IE 中搞砸了(就像一半的 CSS 没有加载)

html - 给 Bootstrap 3 列垂直边距

html - 如何保持最后一个悬停元素处于事件状态?

css - 选中复选框时如何更改 img 样式?

javascript - 为什么在 Firefox 中输入逗号时此正则表达式会清除所有字符?

html - Bootstrap 3 中的中心 <td> 和 <th> 元素

python - 带有 :not does not behave as expected 的 CSS 选择器

html - CSS/HTML child 会覆盖 parent 吗?