html - CSS显示table-caption导致垂直布局

标签 html css

#container {
  width: 960px;
  height: 960px;
  background-color: #ccc;
}
.box {
  width: 300px;
  height: 300px;
  background-color: blue;
  display: table-caption;
  border: 0;
}
<div id='container'>
  <span class='box'>Content</span>
  <span class='box'>Content</span>
  <span class='box'>Content</span>
  <span class='box'>Content</span>
</div>

Table Caption Fiddle Demo

现在,当我将 table-caption 更改为 table-cell 时,它会水平呈现。下面是它的演示。

Table Cell Fiddle Demo

造成不同渲染效果的原因是什么?

最佳答案

这是规范中关于 display: table-caption 的内容。 :

table-caption (In HTML: CAPTION)

Specifies a caption for the table. All elements with 'display: table-caption' must be rendered, as described in section 17.4.

这就是 section 17.4 的内容关于标题框渲染的说明:

The caption boxes are block-level boxes that retain their own content, padding, margin, and border areas, and are rendered as normal block boxes inside the table wrapper box.

enter image description here

关键部分是它们被渲染为普通的 block 框,因此它们每个都显示在另一个下面(就像在它们自己的行中一样)。


其他需要注意的点:(评论中我与 GCyrillus 讨论的总结)

  • 子容器不需要具有 display: table 的父容器即可具有 display: table-celldisplay: table-caption。您可以在 this SO thread 中找到更多详细信息并引用规范的相关部分。
  • 理想情况下,每个表格只有一个标题。用户代理可能不希望在同一个父/表下提供多个标题,这可能解释了为什么 Firefox 的渲染方式与 Chrome 不同。但有关详细信息超出了本答案的范围(在我看来),因为问题仅询问为什么 display: table-caption 导致垂直布局
  • 同意 GCyrillus,在同一父元素下的多个元素上使用 display: table-caption 绝对是不好的做法。我相信您在尝试学习的过程中不断尝试和犯错。

关于html - CSS显示table-caption导致垂直布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35958032/

相关文章:

javascript - 根据选择选项修改元素

javascript - 对 Action 标签感到困惑

php - 如何重新创建此表格效果

windows - -webkit-backface-visibility 在 Windows 上的 node-webkit 中不起作用

html - Chrome 选择列表样式行为过滤选择列表选项

html - 如何在我的导航栏中垂直居中放置 Logo ?

javascript - 在使用 React Transition Group Component 构建轮播时,我缺少什么?

html - 使包含元素的 div 适应不同的分辨率

javascript - 优雅地删除 div 之外的 html 内容

CSS 下拉导航,第 3 级问题