css - Flex 高度在 Chrome 中不起作用

标签 css google-chrome flexbox

<分区>

为了更好地查看问题,请在 firefox 和 chrome 上运行以下 html 代码并查看差异。

该代码适用于所有浏览器,但不适用于 Chrome。问题是当您将显示设置为 flex 并将 flex-direction 设置为 column,并设置其中一个 flex 元素 flex-grow 为 1。此 flex 元素的内容不能将高度设置为 100%。

你能帮我解决不使用 JavaScript 或 css Calc 函数的问题吗? 因为在实际的元素中事情要比这复杂的多。

h1 {
  margin: 0;
  padding: 0;
  color: white;
}

div {
  font-size: 38px;
  color: white;
}
<body style="margin: 0; padding: 0;">
  <div style="height: 100vh; background: royalblue;">
    <div style="display: flex; flex-direction: column; height: 100%;">
      <div style="background: rosybrown; flex-grow: 0;">
        This is first flex item
      </div>
      <div style="flex-grow: 1; background-color: red;">
        <div style="background-color: orange; height: 100%;">
          This div is inside flex item that grows to fill the remaining space. and has css height 100% but its not filling its parent.
          <br/>this div need to be filling its parent (the red div). this works on all other browsers.
        </div>
      </div>
    </div>
  </div>
</body>

最佳答案

height: 100% 添加到橙色 div 的父级:

<div style="flex-grow: 1; background-color: red; height: 100%;"><!-- ADJUSTMENT HERE -->
      <div style="background-color: orange; height: 100%;">
             This div is inside flex item that grows to fill the remaining space.
             and has css height 100% but its not filling its parent.
             <br/>this div need to be filling its parent (the red div).
             this works on all other browsers. 
      </div>
</div>

本质上,Chrome 和 Safari 会根据父元素的 height 属性值来解析百分比高度。 Firefox 和 IE11/Edge 使用父级计算的 flex 高度。有关详细信息,请参阅此答案中的要点 #3:https://stackoverflow.com/a/35051529/3597276

关于css - Flex 高度在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35441475/

相关文章:

html - 如何根据屏幕大小更改背景图像?

html - Flexbox 的 row-reverse 属性是不是把行反转了?

html - 如何固定元素相对于窗口大小的位置

javascript - Chrome 分析器 - 为什么功能有时会停止一小段时间?

html - 为什么我要指定高度 : 0 even if I specified flex-basis: 0 in CSS3 flexbox?

angularjs - 如何使用 Protractor 更改计算机设备的日期和时间

ios - React Native - 精确适配元素

html - Flexbox 不会展开

html - 对齐周围具有相等空间的元素并保持纵横比?

javascript - 无法从 selenium execute_script 访问 chrome 消息传递 API