html - Colgroup 和 Col 在桌面上不可见

标签 html css html-table

我有一个简单的表,我试图在上面添加 colgroupcol 但它们不可见。它也部分破坏了表格的响应能力。

在移动版中,红色列是可见的,但它们是错误的。

这是表格

table {
  border: 1px solid #ccc;
  border-collapse: collapse;
  margin: 0;
  padding: 0;
  width: 100%;
  table-layout: fixed;
}

table caption {
  font-size: 1.5em;
  margin: .5em 0 .75em;
}

table tr {
  background-color: #f8f8f8;
  border: 1px solid #ddd;
  padding: .35em;
}

table th,
table td {
  padding: .625em;
  text-align: center;
}

table th {
  font-size: .85em;
  letter-spacing: .1em;
  text-transform: uppercase;
}

@media screen and (max-width: 600px) {
  table {
    border: 0;
  }

  table caption {
    font-size: 1.3em;
  }
  
  table thead {
    border: none;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
  }
  
  table tr {
    border-bottom: 3px solid #ddd;
    display: block;
    margin-bottom: .625em;
  }
  
  table td {
    border-bottom: 1px solid #ddd;
    display: block;
    font-size: .8em;
    text-align: right;
  }
  
  table td::before {
    content: attr(data-label);
    float: left;
    font-weight: bold;
    text-transform: uppercase;
  }
  
  table td:last-child {
    border-bottom: 0;
  }
}
<table>
  <caption>Statement Summary</caption>
  <colgroup>
    <col span="2" style="background-color:red; text-align: center;">
    <col style="background-color:yellow">
  </colgroup>
  <thead>
    <tr>
      <th scope="col">Account</th>
      <th scope="col">Due Date</th>
      <th scope="col">Amount</th>
      <th scope="col">Period</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-label="Account">Visa - 3412</td>
      <td data-label="Due Date">04/01/2016</td>
      <td data-label="Amount">$1,190</td>
      <td data-label="Period">03/01/2016 - 03/31/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Account">Visa - 6076</td>
      <td data-label="Due Date">03/01/2016</td>
      <td data-label="Amount">$2,443</td>
      <td data-label="Period">02/01/2016 - 02/29/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Account">Corporate AMEX</td>
      <td data-label="Due Date">03/01/2016</td>
      <td data-label="Amount">$1,181</td>
      <td data-label="Period">02/01/2016 - 02/29/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Account">Visa - 3412</td>
      <td data-label="Due Date">02/01/2016</td>
      <td data-label="Amount">$842</td>
      <td data-label="Period">01/01/2016 - 01/31/2016</td>
    </tr>
  </tbody>
</table>

这是 Jsfiddle,您可以在其中查看响应能力:https://jsfiddle.net/d5yfteck/

如何使用 colgroupcol

最佳答案

这一行:

 table tr {
      background-color: #f8f8f8;
    }

正在覆盖 colgroup 定义。

没有那一行你的代码工作正常: your code with colgroup

更新:

如果你想在手机上实现同样的效果, 在您的移动布局中,列显示为行,因此一种解决方案是在媒体标签 (.red .yellow) 中使用类,另请参阅 tbody 应设置为其他颜色。

enter image description here

关于html - Colgroup 和 Col 在桌面上不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56082724/

相关文章:

javascript - WebAudio - 无缝播放音频 block 序列

html - 使下拉菜单可见

css - 在 Ruby on Rails 中使用 slider 输入出生日期

css - 如何混合 CSS 类以获得不同的结果

html - 如何在 <td> 中放置元素?

html - 在 CSS 中,在父元素悬停时显示子元素

css - 如何将 div 中的 <li> 元素对齐到 div 的右侧?

java - mediaCapture windows 8.1 winjs 应用程序

html - 拉伸(stretch)背景时保持内容居中

css - 滚动和表格滚动进入背景时如何固定表格标题