简体   繁体   English

将伪元素放置在父元素后面

[英]Position pseudo element behind parent

I am attempting to place an image (after) behind a parent image. 我正在尝试将图像放置在父图像的后面。 I've used boxes as example of what I am trying to achieve - the blue box is supposed to be behind the green box but no matter what z-index I use it doesn't seem to work. 我以盒子为例来说明要达到的目标-蓝色盒子应该在绿色盒子后面,但是无论我使用什么z-index似乎都行不通。

 .box-wrapper { position: absolute; overflow: hidden; height: 100%; width: 100%; top: 0; z-index: 1; } .box { background-color: green; position: relative; display: block; height: 52px; width: 72px; z-index: 53; top: 2%; z-index: 2; } .box:after { content: ''; //position: relative; background-color: blue; display: block; height: 50px; width: 70px; z-index: -3; } 
 <div class="box-wrapper"> <div class="box"></div> </div> 

Remove the z-index of the box and add position:absolute for box:after 删除box的z-index并在box:after添加position:absolute

.box-wrapper {
  position: absolute;
  overflow: hidden;
  height: 100%;
  width: 100%;
  top: 0;
  z-index: 1;
}
.box {
  background-color: green;
  position: relative;
  display: block;
  height: 52px;
  width: 72px;
  top: 2%;
}
.box:before {
  content: '';
  position: absolute;
  background-color: blue;
  height: 50px;
  width: 70px;
  z-index:-2;
}

jsfiddle jsfiddle

The blue box is supposed to be behind the green box 蓝色框应该在绿色框后面


Just remove all the irrelvant z-index values, then it works perfectly. 只需删除所有无关紧要的z-index值,即可完美运行。

 .box-wrapper { position: absolute; overflow: hidden; height: 100%; width: 100%; top: 0; } .box { background-color: green; position: relative; display: block; height: 52px; width: 72px; top: 2%; color:white; } .box:after { content: ''; position: relative; background-color: blue; display: block; height: 50px; width: 70px; z-index: -1; } 
 <div class="box-wrapper"> <div class="box">I'm on top</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM