css / 伪元素 ::在他的容器后面之前不起作用



我尝试做某事,但它根本不起作用。 这看起来很简单,我希望为具有背景的元素添加:before,但每次我这样做时,::before都会放在容器前面。

.container{
position:relative;
z-index:1;
background:blue;
border:1 px solid white;
}
.container::before{
position:absolute;
content:"";
z-index:-1;
top:-10px;
left:-10px;
width:calc(100% + 20px);
height:calc(100% + 20px);
background:red;
}

只需从.container中删除z-index:1属性

body {
background-color: #eee;
}
.container{
position:relative;
background:blue;
border:1 px solid white;
width: 200px;
height: 200px;
}
.container::before{
position:absolute;
content:"";
z-index:-1;
top:-10px;
left:-10px;
width:calc(100% + 20px);
height:calc(100% + 20px);
background:red;
}
<body>
<div class="container"></div>
</body>

最新更新