html - 为什么 table-caption 会增加表格的高度?

标签 html css css-tables

两个表格的高度都设置为 50px,并且它们的内容没有溢出。但是带有标题的表格实际上是 70px,因为标题似乎没有包含在表格的高度计算中。

谁能解释一下在计算表格高度时不包括标题的合理性?

毕竟是 table 的 child 。如果您想将它排除在表格高度之外,如果您不希望它包含在内,可以将标题放在表格的外面

.table {
  display: table;
  height: 50px;
}
.table-caption {
  display: table-caption;
  background-color: red;
  height: 20px;
}
.table-row {
  display: table-row;
  background-color: green;
}
.table-cell {
  display: table-cell;
}
<div class="table">
  <div class="table-row">
    <div class="table-cell">table height is 50px</div>
  </div>
</div>

<br>
  
<div class="table">
  <div class="table-caption">caption</div>
  <div class="table-row">
    <div class="table-cell">table height is 70px</div>
  </div>
</div>

最佳答案

这在 17.4 Tables in the visual formatting model 中有解释

the table generates a principal block box called the table wrapper box that contains the table box itself and any caption boxes

也就是说,当您在带有 display: table 的元素上设置 height: 50px 时,它适用于表格框本身,不包括标题。

表格包装框确实包含它,因此它的高度是表格框本身的高度 (50px) 加上标题的高度 (20px),即是,70px

enter image description here

关于html - 为什么 table-caption 会增加表格的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35543313/

相关文章:

css - 垂直定位表格单元格元素在 Chrome 中有效,但在 FF 和 IE 中无效,反之亦然。垂直对齐换行文本的替代方法?

javascript - 显示带有下拉按钮的现有 div?

html - 控制表格单元格之间的间距

html - 使用 CSS 平衡表

javascript - 当输入宽度在 Safari 中变小时,表单不会缩小

html - 在行元素 Bootstrap 之间添加边距

html - 如何仅使超大屏幕背景图像变暗

javascript - 单选按钮费用清算

iOS7 HTML5 视频海报不工作

jquery - 创建包含可点击列的 HTML 表格