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

时间:2016-09-14 11:51:02

标签: 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;
}
<div class="or"></div>

4 个答案:

答案 0 :(得分:4)

您可以从父元素中删除z-index,并在伪元素上使用否定z-index: -1。如果您只想要红线上的绿线,则需要从父级DEMO

中删除白色背景

&#13;
&#13;
.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;
&#13;
&#13;

答案 1 :(得分:2)

伪元素被视为其相关元素的后代。因此,.or::after将从z-index: 11继承.or

只需从z-index移除.or并在z-index: -1上更新.or::after

&#13;
&#13;
.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;
&#13;
&#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

&#13;
&#13;
.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;
&#13;
&#13;