html - 将伪元素放在父元素后面

标签 html css sass

我正在尝试将图像(之后)放在父图像的后面。我使用盒子作为我想要实现的目标的示例 - 蓝色盒子应该在绿色盒子后面,但无论我使用什么 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>

最佳答案

移除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

关于html - 将伪元素放在父元素后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39021637/

相关文章:

html - 如何对齐 block 引用的开引号和闭引号?

css - 将 bootstrap.scss 导入 styles.scss 比添加到 .angular.cli.json 中的样式 block 有什么优势?

html - CSS3 箭头不显示

javascript - Angular JS - 错误 : ng:areq Bad Argument Argument 'GameContoller' is not a function, 未定义

html - 纯CSS结合两个动画

css - 如何在单行中制作元素?

html - 导航栏 HTML CSS 中的下拉菜单中没有任何内容

typescript - 在Vue中将scss变量导入 typescript

Java - 从 URL 的视频中获取片段

jquery - Facebook 的行情栏和聊天侧边栏中的水平滚动条