gpt4 book ai didi

html - Flexbox、z-index 和位置 : static: Why isn't it working?

转载 作者:技术小花猫 更新时间:2023-10-29 12:32:19 25 4
gpt4 key购买 nike

根据 flexbox 规范:

..4.3. Flex Item Z-Ordering ,... and z-index values other than auto create a stacking context even if position is static.

所以,我认为 z-index/opacity 应该像往常一样与 flexbox 一起工作,但是当我将它应用到这个 HTML/CSS 时它不起作用(我的目标是将 .header 放在 .core 之上,创建两层):

 .header {
opacity: 0.5;
z-index: 2;
display: flex;
align-items: center;
justify-content: flex-end;
}
.headerLeft {
z-index: inherit;
background-color: blue;
text-align: right;
align-self: stretch;
flex: 2 1 250px;
}
.headerCenter {
z-index: inherit;
background-color: red;
text-align: right;
align-self: stretch;
flex: 1 1 175px;
}
.headerRight {
z-index: inherit;
background-color: green;
text-align: right;
align-self: stretch;
flex: 1 1 100px;
}
.core {
z-index: 0;
display: flex;
flex-flow: row wrap;
align-items: center;
justify-content: space-around;
}
.coreItem {
align-self: stretch;
flex: 1 1 400px;
}
<body>
<div class="header">
<div class="headerLeft">Left</div>
<div class="headerCenter">Center</div>
<div class="headerRight">Right</div>
</div>
<div class="core">
<div class="coreItem">Core1</div>
<div class="coreItem">Core2</div>
<div class="coreItem">Core3</div>
<div class="coreItem">Core4</div>
<div class="coreItem">Core5</div>
<div class="coreItem">Core6</div>
</div>
</body>

我在 flex 属性上使用了适当的前缀。我不明白为什么它不起作用。

最佳答案

就像您在问题中所写的那样,元素不需要需要为 z-index 定位才能在 flex 容器中工作。

即使使用 position: static,Flex 元素也可以参与 z-index 堆叠顺序,这对于其他 CSS 盒模型(Grid 除外)是不正确的,其中 z-index 仅适用于定位元素。

4.3. Flex Item Z-Ordering

Flex items paint exactly the same as inline blocks, except that order-modified document order is used in place of raw document order, and z-index values other than auto create a stacking context even if position is static.

z-index 在您的代码中不起作用的原因是 div.headerdiv.core 不是 flex 元素。它们是 body 的子级,但 body 不是 flex 容器。

为了让 z-index 在这里工作,您需要将 display: flex 应用到 body

将此添加到您的代码中:

body {
display: flex;
flex-direction: column;
}

Revised Demo

更多详情:https://stackoverflow.com/a/35772825/3597276

关于html - Flexbox、z-index 和位置 : static: Why isn't it working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33391370/

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