z-index 不适用于 ::after/::before 元素?

Jim*_*had 6 css

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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="or"></div>
Run Code Online (Sandbox Code Playgroud)

Nen*_*car 6

您可以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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="or"></div>
Run Code Online (Sandbox Code Playgroud)