【问题标题】:100% height div inside table-cell not working on Firefox表格单元格内的 100% 高度 div 在 Firefox 上不起作用
【发布时间】:2015-04-13 17:35:01
【问题描述】:

我正在尝试使用 display table-cell 属性创建一个两列 100% 高度的布局。它在 Chrome 上运行良好,但我在 Firefox 和 IE 上都没有成功。

这里是小提琴:http://jsfiddle.net/ehfa0kk8/5/

看看它是如何在 Chrome 上运行的。知道如何进行这项工作吗?

<div id="table">
    <div id="row">
        <div id="cell_1">
            <div id="overflown_div">
                long content goes here...
            </div>
        </div>
        <div id="cell_2">
            Blah blah blah
        </div>
    </div>
</div>

还有 CSS

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

#table {
    width: 100%;
    display: table;
    height: 100%;
}

#row {
    display: table-row;
}

#cell_1, #cell_2 {  
    display: table-cell;
    height: 100%;
}

#cell_1 {
    width: 390px;
    background: aliceblue;
}

#cell_2 {
    background: yellow;
}

#overflown_div {
    height: 100%;
    overflow: scroll;
    padding: 10px;
}

更新:首先,左列应该有足够的内容,这样它就会溢出。在 Chrome 上,它会显示一个滚动条,因此您可以只滚动该列(单元格)的内容。在 Firefox 上不会发生。

示例:

【问题讨论】:

  • 什么不起作用?我在 Firefox 中看到 2 个全高列。
  • 首先,我不知道你的屏幕有多大,但是左栏应该有很长的内容,所以它会溢出。在 Chrome 上,它会显示一个滚动条,因此您可以只滚动该列(单元格)的内容。在 Firefox 上不会发生这种情况
  • 在 Firefox 上看起来不错,你能发一个截图来解释这个问题吗?
  • @sdcr,我刚刚做到了。请注意 Chrome 上溢出 div 上的滚动条,但在 Firefox 上 100% 高度不起作用。

标签: html css


【解决方案1】:

诀窍是:

  • 将行的height 设置为100%
  • 将单元格的height 设置为0

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#table {
  width: 100%;
  display: table;
  height: 100%;
}
#row {
  display: table-row;
  height: 100%;
}
#cell_1,
#cell_2 {
  display: table-cell;
  height: 0;
}
#cell_1 {
  width: 390px;
  background: aliceblue;
}
#cell_2 {
  background: yellow;
}
#overflown_div {
  height: 100%;
  overflow-y: scroll;
  padding: 10px;
  box-sizing: border-box;
}
#overflown_div p {
  height: 80px;
}
<div id="table">
  <div id="row">
    <div id="cell_1">
      <div id="overflown_div">
        <p>Blah blah blah</p>
        <p>Blah blah blah</p>
        <p>Blah blah blah</p>
        <p>Blah blah blah</p>
        <p>Blah blah blah</p>
        <p>Blah blah blah</p>
        <p>Blah blah blah</p>
        <p>Blah blah blah</p>
        <p>Blah blah blah</p>
        <p>Blah blah blah</p>
      </div>
    </div>
    <div id="cell_2">
      Blah blah blah
    </div>
  </div>
</div>

【讨论】:

  • 它适用于 Firefox,但不适用于 IE。这背后有什么逻辑吗?
  • 如果您将两者都设置为 100% 并且仅针对 Firefox,则它可以工作。另一方面,Chrome 需要单元格的高度固定,甚至为零。完整的解决方案是:th, td { height: 0; } @-moz-document url-prefix() { tr, th, td { height: 100%; } }(按顺序使用)
猜你喜欢
  • 2012-04-14
  • 2015-05-24
  • 2013-01-09
  • 2017-12-24
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
相关资源
最近更新 更多