gpt4 book ai didi

html - Chrome 中 100% 高度溢出主体的 Div

转载 作者:可可西里 更新时间:2023-11-01 13:29:04 26 4
gpt4 key购买 nike

我正在尝试按照描述制作 100% 高度 flex 的 child herehere .在 firefox 和 IE 中它按预期显示,但在 chrome 中它被搞砸了。 div 正在脱离 body 。这是 chrome 遵循规范的另一个副作用还是仍然是一个错误?

代码:

<!DOCTYPE html>
<html>
<head>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
div {
border: 1px solid black;
box-sizing: border-box;
margin: 0;
padding: 0;
}

.red-border {
border: 2px solid red;
}

.blue-border {
border: 2px solid blue;
}

.green-border {
border: 2px solid green;
}
</style>
</head>
<body class="red-border" >
<div clsas="blue-border" style="display: flex; flex-direction: column; width: 100%; height: 100%;">
<div style="flex: 0 1 auto; ">top</content></div>

<div class="green-border" style="flex: 1 1 auto; display: flex; flex-direction: row; width: 100%; height: 100%;" >
<div style="flex: 0 1 auto; ">left</div>
<div style="flex: 1 1 auto; width: 100%; height: 100%;">
<div style="background-color: #ff6500; width: 100%; height: 100%;">center</div>
</div>
<div style="flex: 0 1 auto; ">right</div>
</div>

<div style="flex: 0 1 auto; ">bottom</content></div>
</div>
</body>
</html>

plnkr: http://run.plnkr.co/plunks/wi5MQYK5yEdzdgQBaUWh/

这就是它在 ff 和 ie (11) 中的样子: that's how it looks in ff

这就是它在 chome 中的样子: that's how it looks in chome

更新:“中心”div 应该是非 flex div,并且仍然必须具有 100% 的高度。使用非 flex div 的原因是我的真实站点结构非常复杂——它使用了很多 webcomponents,其中一些只是不能使用 flex。所以在某些时候我必须停止使用 flex 并让“正常”div 具有 100% 的高度。

最佳答案

问题似乎源于您将 height 组合在一起的事实和 flex-basis同一声明 block 中的属性。它似乎制造了冲突。在我下面的回答中,我删除了高度并使用了 flex-basis反而。您还会注意到其他一些调整。


变化:

(1)

<div class="green-border"
style="flex: 1 1 auto; display: flex; flex-direction: row; width: 100%; height: 100%;" >

<div class="green-border"
style="flex: 1 1 100%; display: flex; flex-direction: row; width: 100%;" >

注释:已删除 height ;二手 flex-basis相反;


(2)

<div style="flex: 1 1 auto; width: 100%; height: 100%;">

<div style="flex: 1 1 100%; width: 100%; display: flex;">

注释:已删除 height ;二手 flex-basis反而;已建立嵌套 flex 容器;


(3)

<div style="background-color: #ff6500; width: 100%; height: 100%;">center</div>

<div style="background-color: #ff6500; width: 100%; flex: 1 1 100%;">center</div>

注释:已删除 height ;二手 flex-basis反而;添加 flex属性(property);


最后,由于打字错误,蓝色边框没有显示。

(4)

clsas="blue-border"

class="blue-border"


通过上述调整,布局在 Chrome 中呈现如下:

enter image description here

所有盒子都在它们的容器内。

DEMO


将 100% 高度应用于嵌套的非 flex 元素

在大多数情况下,在子元素上使用百分比高度时,还必须为父元素和所有祖先元素指定百分比高度,直至并包括根元素。

html, body { height: 100%; }

我在这里解释了原因:

但是仔细看看spec显示异常:

The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'.

注意这部分:...并且元素不是绝对定位的。

因此,在“中心”框上试试这个:

变化:

(5)

<div style="background-color: #ff6500; width: 100%; flex: 1 1 100%;">center</div>

<div style="background-color: #ff6500; width: 100%; flex: 1 1 100%; position: relative;">
<div style="border: 5px solid yellow; position: absolute; height: 100%; width: 100%;">
center</div>
</div>

注意事项:

  • 已添加 position: relative为绝对定位建立最近的定位祖先
  • 创建了新的、嵌套的、非 flex 的 div容器
  • 将绝对定位应用于新的 divheight: 100% .

使用绝对定位,您不需要将百分比高度应用于父容器。

DEMO

关于html - Chrome 中 100% 高度溢出主体的 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32945688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com