使用 css 在 上设置强制宽度,该宽度位于 下,使用 colspan 而不使用 colgroup

2023-12-03

我想设置宽度td in tbody在下面thead th有一个colspan="2"具有硬定义的列宽(以 % 为单位)。如果单元格内容超出,浏览器外壳不会动态调整表格宽度。

.sample {
  width: 100%;
  table-layout: fixed;
}

.sample td:nth-child(1),
.sample th:nth-child(1) {
  width: 30%;
}

.sample td:nth-child(2),
.sample th:nth-child(2) {
  width: 70%;
}
<table class="sample" border="1">
  <thead>
    <tr>
      <th colspan="2">Header</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>30% This columns has a lot of content and a really long woooooooooooooooooooooooooooooooooooooord which is not wrapable by the browser by default. This column still should be 30%with.</td>
      <td>70%</td>
    </tr>
  </tbody>
</table>

上面的样式有效,如果thead已删除但无法使用它。

重要笔记,

  • 我添加了table-layout: fixed;因为我需要列的宽度与指定的宽度完全相同。使用table-layoud: auto;导致浏览器将指定宽度作为方向,但如果表格内容超出可用宽度,仍然动态调整宽度。
  • colgroup and col无法使用,因为我使用的是所见即所得编辑器,它不添加 colgroups。因此我依赖CSS。

这是一个后续问题,因为在我最初的问题中出现了新的情况使用 colspan 设置

下 的宽度

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 css 在 上设置强制宽度,该宽度位于 下,使用 colspan 而不使用 colgroup 的相关文章

随机推荐