z-index 父元素后面的伪元素 box-shadow

z-index a pseudo-element box-shadow behind a parent element

我正在尝试将 :before:after box-shadow 放在按钮后面。但是过渡是从 a 标签前面开始的。对于我的作品 CMS,我需要所有属性都在 a 标签上。

<a href="#" class="btn">Join Today</a>


.btn{
  position: relative;
  z-index: 1;
  display: inline-block;
  text-align: center;
  background-color: #8ec656;
  border-radius: 30px;
  font-family: 'Roboto Condensed', sans-serif;
  font-size: 20px;
  color: #fff;
  border: 3px #d2d2d2 solid;
  padding: 15px 40px;
  line-height: 1;
  transition: box-shadow .4s;
}
.btn:hover{
  background: #6b9640;
  box-shadow: inset 0px -22px 13px 0px #84b652, inset 0px 2px 5px 0px #84b652;
}
.btn:before,
.btn:after{
  content: "";
  position: absolute;
  z-index: -1;
  bottom: 9px;
  left: 10px;
  width: 88%;
  top: 73%;
  transition: box-shadow .4s;
}
.btn:hover:before,
.btn:hover:after{
  box-shadow: 0 15px 10px #777;
}

https://jsfiddle.net/3aj5muyu/

那是因为.btn建立了一个堆叠上下文,所以它的背景总是在它的内容的后面。 back-to-front order

  1. the background and borders of the element forming the stacking context.
  2. the child stacking contexts with negative stack levels (most negative first).
  3. the in-flow, non-inline-level, non-positioned descendants.
  4. the non-positioned floats.
  5. the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
  6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
  7. the child stacking contexts with positive stack levels (least positive first).

要修复它,.btn 不应建立堆栈上下文。将以下属性移至包装器:

position: relative;
z-index: 1;

.btn-wrapper {
  position: relative;
  z-index: 1;
  display: inline-block;
}
.btn{
  display: inline-block;
  text-align: center;
  background-color: #8ec656;
  border-radius: 30px;
  font-family: 'Roboto Condensed', sans-serif;
  font-size: 20px;
  color: #fff;
  border: 3px #d2d2d2 solid;
  padding: 15px 40px;
  line-height: 1;
  transition: box-shadow .4s;
  text-decoration: none;
}
.btn:hover{
  background: #6b9640;
  box-shadow: inset 0px -22px 13px 0px #84b652, inset 0px 2px 5px 0px #84b652;
}
.btn:before,
.btn:after{
  content: "";
  position: absolute;
  z-index: -1;
  bottom: 9px;
  left: 10px;
  width: 88%;
  top: 73%;
  transition: box-shadow .4s;
}
.btn:hover:before,
.btn:hover:after{
  box-shadow: 0 15px 10px #777;
}
<div class="btn-wrapper">
  <a href="#" class="btn">Join Today</a>
</div>

看看这个工作示例:

.btn {
  position: relative;
  /* z-index: 1; */
  display: inline-block;
  
  background-color: #8ec656;
  border-radius: 30px;
  border: 3px #d2d2d2 solid;
  padding: 15px 40px;
  transition: box-shadow .4s;
  text-decoration: none;
}

.btn > span {
  text-align: center;
  font-family: 'Roboto Condensed', sans-serif;
  font-size: 20px;
  color: #fff;
  line-height: 1;
}

.btn:hover{
  background: #6b9640;
  box-shadow: inset 0px -22px 13px 0px #84b652, inset 0px 2px 5px 0px #84b652;
}
.btn:before,
.btn:after{
  display: inline-block;
  content: "";
  position: absolute;
  z-index: -1;
  bottom: 9px;
  left: 10px;
  width: 88%;
  top: 73%;
  transition: box-shadow .4s;
}
.btn:hover:before,
.btn:hover:after{
  box-shadow: 0 15px 10px #777;
}
<a href="#" class="btn"><span>Join Today</span></a>

https://jsfiddle.net/7argwcje/

那是因为 before 和 after 元素实际上是 .btn 元素的子元素和纯文本的兄弟元素(今天加入)。所以你没有办法强制文本在前面。

这些是变化:

.btn {
  position: relative;
  /* z-index: 1; */
  display: inline-block;
  background-color: #8ec656;
  border-radius: 30px;
  border: 3px #d2d2d2 solid;
  padding: 15px 40px;
  transition: box-shadow .4s;
  text-decoration: none;
}

.btn > span {
  text-align: center;
  font-family: 'Roboto Condensed', sans-serif;
  font-size: 20px;
  color: #fff;
  line-height: 1;
}

基本上将一些属性从 .btn 转移到包装跨度元素。

请注意,不再需要 z-index。

.btn:before,
.btn:after{
  transition: .6s;
  content: "";
  box-shadow: 0 28px 17px -3px #777;
  width: 84%;
  height: 11px;
  top: 48%;
  left: 15px;
  position: absolute;
  opacity: 0;
}
.btn:hover:before,
.btn:hover:after{
  opacity: 1;
}

这是我不得不接受的。谢谢您的反馈!