【问题标题】:Height not being respected in absolutely positioned table在绝对定位的桌子上不尊重高度
【发布时间】:2017-07-07 18:40:27
【问题描述】:

如果带有display: table 的绝对定位元素明确设置了高度,如果内容高度大于提供的高度值,则该元素将收缩包裹内容而不考虑高度值。

#main {
  width: 100px;
  height: 29px;
  position: absolute;
  top: 20px;
  left: 200px;
  border: 1px red dashed;
  display: table;
  table-layout: fixed;
  border-spacing: 0;
  border-collapse: separate;
}

#child {
  display: table-cell;
}
<div id="main">
  <div id="child">Custom Text With Validation:</div>
</div>

【问题讨论】:

  • 那是真的......那么有一个与这个事实相关的问题吗?
  • 是的,问题是,为什么不尊重高度值。它是明确设置的,并将在 display: 块中得到尊重。看来 display:table 和 position: absolute 有什么特殊关系?

标签: html css


【解决方案1】:

表格单元格不尊重其父级的高度,原因与我不尊重 max-height 的原因相同,因此会推高表格的大小。

这里有 2 种方法可以使它工作,或者在 child 上设置 position: absolute

#main {
  width: 100px;
  height: 29px;
  position: absolute;
  top: 20px;
  left: 200px;
  border: 1px red dashed;
  display: table;
  table-layout: fixed;
  border-spacing: 0;
  border-collapse: separate;
}
#child {
  position: absolute;
  display: table-cell;
}
<div id="main">
  <div id="child">Custom Text With Validation:
  </div>
</div>

或在child 中添加一个额外的元素并在其上设置height。

#main {
  width: 100px;
  height: 29px;
  position: absolute;
  top: 20px;
  left: 200px;
  border: 1px red dashed;
  display: table;
  table-layout: fixed;
  border-spacing: 0;
  border-collapse: separate;
}
#child {
  display: table-cell;
}
#child div {
  height: 29px;
}
<div id="main">
  <div id="child">
    <div>Custom Text With Validation:</div>
  </div>
</div>

在桌子上添加overflow: hidden 也会正确“切掉”多余的部分。

【讨论】:

  • 是否有任何规范解释这种行为?
  • @whowhenhow 是和否。如果您阅读本文,https://www.w3.org/TR/CSS2/tables.html#table-display,您会发现很多 Note。未来的 CSS 更新可能会进一步说明这一点。 早在很久以前,表格元素的规范就很弱。每个浏览器都可以并且正在解释这些有所不同。
  • 这对我来说似乎是一个错误,因为 display: table 表示块级元素。由于高度是明确设置的,没有人应该覆盖它。如果不使用包装器,我们将无法控制定位表的高度。
  • @whowhenhow display: table 不像普通的块元素,它表现为具有一些块元素特性的表格元素。例如,一个表格会将一个内联元素推送到下一行,尽管它本身不会占用全宽,除非将其设置为 100%。你会说这也是一个错误,因为块元素确实需要全宽而不将其设置为 100%?
  • 在上面的情况下我们仍然可以控制宽度。这是一个错误吗?也许还有stackoverflow.com/questions/25048236/…
猜你喜欢
  • 1970-01-01
  • 2012-07-08
  • 2015-07-22
  • 2011-10-16
  • 1970-01-01
  • 2014-08-27
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多