gpt4 book ai didi

css - 伪元素 : proper z-index stacking context?

转载 作者:行者123 更新时间:2023-11-28 05:09:00 25 4
gpt4 key购买 nike

我在同一层有两个 DIV 元素:barnav 以及 ::before 伪元素.

那个 navbar 应该出现在 ::before 伪元素下,但文本在 nav 上面,像这样:

|- nav text
|- nav pseudo element
|- bar

*,
*::after,
*::before {
box-sizing: border-box;
margin: 0;
}
header {
position: relative;
}
.bar {
color: white;
background-color: black;
}
.nav {
background-color: orange;
}
p {
color: white;
margin-left: 10%;
}
.nav::before {
z-index: 0;
content: "";
position: absolute;
top: 0;
left: 0;
right: 40%;
bottom: 0;
height: 0;
border-style: solid;
border-width: 4rem 4rem 0 0;
border-color: green transparent transparent transparent;
}
<header>
<div class="bar">bar that should remain under green</div>
<div class="nav">
<p>text that should appear above green</p>
</div>
</header>

最佳答案

只需将相同的属性添加到文本即可。

*,
*::after,
*::before {
box-sizing: border-box;
margin: 0;
}
header {
position: relative;
}
.bar {
color: white;
background-color: black;
}
.nav {
background-color: orange;
}
p {
color: white;
margin-left: 10%;
z-index: 1;
position: relative;
}
.nav::before {
z-index: 0;
content: "";
position: absolute;
top: 0;
left: 0;
right: 40%;
bottom: 0;
height: 0;
border-style: solid;
border-width: 4rem 4rem 0 0;
border-color: green transparent transparent transparent;
}
<header>
<div class="bar">bar that should remain under green</div>
<div class="nav">
<p>text that should appear above green</p>
</div>
</header>

关于css - 伪元素 : proper z-index stacking context?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39678740/

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