首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在CSS中设置表子标题的宽度

在CSS中设置表子标题的宽度
EN

Stack Overflow用户
提问于 2018-10-22 12:02:05
回答 2查看 322关注 0票数 1

我有一张像上面所示的桌子,它同时使用了大跨度和大跨度。我想修复所有的列宽。我可以做第一和最后(否和备注),我可以设置类型的宽度。如何指定A、B和C的宽度?

我尝试给单元格A,B和C CSS类和设置单独的宽度,但这没有任何影响。宽度始终由下面单元格中的任何内容控制。

代码语言:javascript
复制
table {
  border-collapse:collapse;
  table-layout:fixed;
  width:100%;
}

th, td {
  border:solid 1px #000;
  padding:.5em;
}

.no {
  width:10%;
}

.type {
  width:50%;
}

.remark {
  width:40%;
}

/* these don't work */

.type-a {
  width:10%;
}

.type-b {
  width:15%;
}

.type-c {
  width:25%;
}
代码语言:javascript
复制
<table>
  <thead>
    <tr>
      <th rowspan="2" class="no">No</th>
      <th colspan="3" class="type">Type</th>
      <th rowspan="2" class="remark">Remark</th>
    </tr>
    <tr>
      <th class="type-a">A</th>
      <th class="type-b">B</th>
      <th class="type-c">C</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>

在细胞内放置固定宽度的div是可能的吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-22 13:19:08

将表布局属性更改为auto:

代码语言:javascript
复制
table {
  border-collapse:collapse;
  table-layout: auto;
  width:100%;
}

th, td {
  border:solid 1px #000;
  padding:.5em;
}

.no {
  width:10%;
}

.type {
  width:50%;
}

.remark {
  width:40%;
}

/* these don't work */

.type-a {
  width:10%;
}

.type-b {
  width:15%;
}

.type-c {
  width:25%;
}
代码语言:javascript
复制
<table>
  <thead>
    <tr>
      <th rowspan="2" class="no">No</th>
      <th colspan="3" class="type">Type</th>
      <th rowspan="2" class="remark">Remark</th>
    </tr>
    <tr>
      <th class="type-a">some long text to see the width remains the same </th>
      <th class="type-b">B</th>
      <th class="type-c">C</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>

票数 2
EN

Stack Overflow用户

发布于 2018-10-22 13:10:07

更改表-布局:自动

代码语言:javascript
复制
table {
  border-collapse:collapse;
  table-layout:auto;
  width:100%;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52928862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档