gpt4 book ai didi

html - 绝对定位的 flex 元素不会从 IE11 的正常流程中移除

转载 作者:行者123 更新时间:2023-11-28 04:56:01 25 4
gpt4 key购买 nike

我们有两个包含内容的 div 和第三个 div,它是一个具有绝对位置的背景。

容器是一个 flexbox。

在 Chrome 和 Safari 中一切正常,但 Firefox 和 IE11 考虑绝对定位的 div,并在 div 之间分配空间,就像连续 3 个 div。

enter image description here

我制作了 jsfiddle 示例。有什么办法可以修复这个错误吗? https://jsfiddle.net/s18do03e/2/

div.container {
display: flex;
flex-direction: row;
width: 100%;
height: 300px;
justify-content: space-between;
width: 100%;
outline: 1px solid;
}
div.c1 {
background: #aaeecc;
width: 100px;
position: relative;
z-index: 50;
top: 20px;
display: flex;
}
div.c2 {
background: #cceeaa;
width: 200px;
position: relative;
z-index: 50;
top: 20px;
display: flex;
}
div.bg {
background: #ccc;
width: 100%;
height: 100%;
z-index: 0;
left: 0px;
top: 0px;
position: absolute;
display: flex;
}
<div class="container">
<div class="c1">Content 1</div>
<div class="c2">Content 2</div>
<div class="bg">Background</div>
</div>

最佳答案

更新:此问题已在 Firefox 中解决(截至 2017 年 3 月发布的 v52)。问题在IE11中依然存在。


就像你在问题中写的那样:

Firefox calculates absolute positioned div, and distributes space between divs like there are 3 divs in a row.

Firefox 正在考虑第三个 div (.bg),它是绝对定位的 in-flow flex 元素,并将其分解为 space-between 计算。 (IE11 也这样做;Chrome 和 Edge 忽略它。)

显然,这不符合当前的 flexbox 规范:

4.1. Absolutely-Positioned Flex Children

As it is out-of-flow, an absolutely-positioned child of a flex container does not participate in flex layout.

这里有一些解决方法:

为什么不在其他两个之间移动绝对定位的 div?

取而代之的是:

<div class="container">
<div class="c1">Content 1</div>
<div class="c2">Content 2</div>
<div class="bg">Background</div>
</div>

试试这个:

<div class="container">
<div class="c1">Content 1</div>
<div class="bg">Background</div>
<div class="c2">Content 2</div>
</div>

div.container {
display: flex;
flex-direction: row;
width: 100%;
height: 300px;
justify-content: space-between;
width: 100%;
outline: 1px solid;
}
div.c1 {
background: #aaeecc;
width: 100px;
position: relative;
z-index: 50;
top: 20px;
display: flex;
}
div.c2 {
background: #cceeaa;
width: 200px;
position: relative;
z-index: 50;
top: 20px;
display: flex;
}
div.bg {
background: #ccc;
width: 100%;
height: 100%;
z-index: 0;
left: 0px;
top: 0px;
position: absolute;
display: flex;
}
<div class="container">
<div class="c1">Content 1</div>
<div class="bg">Background</div>
<div class="c2">Content 2</div>
</div>

或...从 flex 容器中删除 .bg:

<div class="container">
<div class="c1">Content 1</div>
<div class="c2">Content 2</div>
</div>
<div class="bg">Background</div>

div.container {
display: flex;
flex-direction: row;
width: 100%;
height: 300px;
justify-content: space-between;
width: 100%;
outline: 1px solid;
}
div.c1 {
background: #aaeecc;
width: 100px;
position: relative;
z-index: 50;
top: 20px;
display: flex;
}
div.c2 {
background: #cceeaa;
width: 200px;
position: relative;
z-index: 50;
top: 20px;
display: flex;
}
div.bg {
background: #ccc;
width: 100%;
height: 100%;
z-index: 0;
left: 0px;
top: 0px;
position: absolute;
display: flex;
}
<div class="container">
<div class="c1">Content 1</div>
<div class="c2">Content 2</div>
</div>
<div class="bg">Background</div>

或者...使用 flex order 属性重新排列 flex 元素。

将此添加到您的代码中:

.c2 { order: 1; }

div.container {
display: flex;
flex-direction: row;
width: 100%;
height: 300px;
justify-content: space-between;
width: 100%;
outline: 1px solid;
}
div.c1 {
background: #aaeecc;
width: 100px;
position: relative;
z-index: 50;
top: 20px;
display: flex;
}
div.c2 {
background: #cceeaa;
width: 200px;
position: relative;
z-index: 50;
top: 20px;
display: flex;
order: 1;
}
div.bg {
background: #ccc;
width: 100%;
height: 100%;
z-index: 0;
left: 0px;
top: 0px;
position: absolute;
display: flex;
}
<div class="container">
<div class="c1">Content 1</div>
<div class="c2">Content 2</div>
<div class="bg">Background</div>
</div>

关于html - 绝对定位的 flex 元素不会从 IE11 的正常流程中移除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40397386/

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