z-index不适用于其::before
/ ::after
元素。我在这里分享以下代码。
.or {
border: 2px solid #8fc300;
border-radius: 5px;
color: #333;
font-size: 17px;
font-weight: 600;
height: 34px;
line-height: 26px;
text-align: center;
width: 34px;
margin-top: 64px;
margin-left: 20px;
margin-right: 20px;
background: #fff;
/*For z-index - keep the green area on top*/
position: relative;
z-index: 11;
}
.or::after {
background: red;
content: "";
display: block;
height: 116px;
margin-left: 15px;
margin-top: -68px;
width: 4px;
/*For z-index - keep the green area on top*/
position: relative;
z-index: 9;
}
<div class="or"></div>
答案 0 :(得分:4)
您可以从父元素中删除z-index
,并在伪元素上使用否定z-index: -1
。如果您只想要红线上的绿线,则需要从父级DEMO
.or {
border: 2px solid #8fc300;
border-radius: 5px;
color: #333;
font-size: 17px;
font-weight: 600;
height: 34px;
line-height: 26px;
text-align: center;
width: 34px;
margin-top: 64px;
margin-left: 20px;
margin-right: 20px;
background: #fff;
/*For z-index - keep the green area on top*/
position: relative;
}
.or::after {
background: red;
content: "";
display: block;
height: 116px;
margin-left: 15px;
margin-top: -68px;
width: 4px;
/*For z-index - keep the green area on top*/
position: absolute;
z-index: -1;
}
&#13;
<div class="or"></div>
&#13;
答案 1 :(得分:2)
伪元素被视为其相关元素的后代。因此,.or::after
将从z-index: 11
继承.or
。
只需从z-index
移除.or
并在z-index: -1
上更新.or::after
.or {
border: 2px solid #8fc300;
border-radius: 5px;
color: #333;
font-size: 17px;
font-weight: 600;
height: 34px;
line-height: 26px;
text-align: center;
width: 34px;
margin-top: 64px;
margin-left: 20px;
margin-right: 20px;
background: #fff;
/*For z-index - keep the green area on top*/
position: relative;
}
.or::after {
background: red;
content: "";
display: block;
height: 116px;
margin-left: 15px;
margin-top: -68px;
width: 4px;
/*For z-index - keep the green area on top*/
position: relative;
z-index: -1;
}
&#13;
<div class="or"></div>
&#13;
答案 2 :(得分:2)
这是一个可能的解决方案。
在.or
元素上,不要设置z-index值并删除白色背景(但这可能不是你需要的)。
在:after
伪元素上,设置z-index: -1
,这将导致它在父元素下呈现(绘制)。
.or {
border: 6px solid #8fc300;
border-radius: 5px;
color: #333;
font-size: 17px;
font-weight: 600;
height: 34px;
line-height: 26px;
text-align: center;
width: 34px;
margin-top: 64px;
margin-left: 20px;
margin-right: 20px;
/*background: #fff;*/
/*For z-index - keep the green area on top*/
position: relative;
/*z-index: 11;*/
}
.or::after {
background: red;
content: "";
display: block;
height: 116px;
margin-left: 15px;
margin-top: -68px;
width: 4px;
/*For z-index - keep the green area on top*/
position: relative;
z-index: -1;
}
<div class="or"></div>
答案 3 :(得分:1)
如果您想要这样的话,试试这个。我确实更改了.or
类的样式,例如background-color: transparent
删除了位置,z-index和.or::after
z-index: -11
.or {
border: 2px solid #8fc300;
border-radius: 5px;
color: #333;
font-size: 17px;
font-weight: 600;
height: 34px;
line-height: 26px;
text-align: center;
width: 34px;
margin-top: 64px;
margin-left: 20px;
margin-right: 20px;
background: transparent;
/*For z-index - keep the green area on top*/
/*position: relative;*/
/*z-index: 11;*/
}
.or::after {
background: red;
content: "";
display: block;
height: 116px;
margin-left: 15px;
margin-top: -68px;
width: 4px;
/*For z-index - keep the green area on top*/
position: relative;
z-index: -11;
}
&#13;
<div class="or"></div>
&#13;