为什么z-index:-1不在这个实例中工作?

时间:2015-01-16 16:24:25

标签: html css

我需要<i>元素在堆叠顺序中显示在锚点下方,但似乎没有任何效果。建议?

a.button {
  background-color: gray;
  border-radius: 5px;
  color: rgb(247, 247, 247);
  display: inline-block;
  font-family: "Helvetica Neue", Verdana, Helvetica, Arial, sans-serif;
  font-size: 12px;
  font-style: normal;
  font-weight: 800;
  height: 26px;
  line-height: 26px;
  padding: 0 12px;
  position: relative;
  text-align: center;
  text-decoration: none;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
  z-index: 1;
}
a.button i {
  background: rgb(128, 0, 128);
  border-radius: 5px;
  border-top-right-radius: 2.5px;
  height: 20px;
  left: 80%;
  position: absolute;
  top: 50%;
  width: 20px;
  z-index: -1;
}
<a class="button" href="#"><i></i>Button</a>

z-index: 1中删除<a>会导致<i>出现在父元素下方:

div.wrapper {
  background: pink;
  padding: 20px;
}
a.button {
  background-color: gray;
  border-radius: 5px;
  color: rgb(247, 247, 247);
  display: inline-block;
  font-family: "Helvetica Neue", Verdana, Helvetica, Arial, sans-serif;
  font-size: 12px;
  font-style: normal;
  font-weight: 800;
  height: 26px;
  line-height: 26px;
  padding: 0 12px;
  position: relative;
  text-align: center;
  text-decoration: none;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
}
a.button i {
  background: rgb(128, 0, 128);
  border-radius: 5px;
  border-top-right-radius: 2.5px;
  height: 20px;
  left: 80%;
  position: absolute;
  top: 50%;
  width: 20px;
  z-index: -1;
}
<div class="wrapper">
  <a class="button" href="#"><i></i>Button</a>
</div>

1 个答案:

答案 0 :(得分:1)

在不更改HTML的情况下,您无法以可靠的方式执行此操作。如果这是一个选择,那很好。否则,i元素的堆叠上下文与a.button的堆叠上下文相同,并且无法通过CSS更改。