是否可以在同一个div中使用不同的z-index?

问题描述 投票:-1回答:3

我想要两次相同的文本,一个在另一个之上。此文本由图像剪切。

这些文本位于居中的同一个div中,但它们的z-index保持不变。

我试图改变我元素的任何z-index。

.bottom {
  font-family: Gotham;
  font-style: normal;
  font-weight: 900;
  font-size: 144px;
  color: #ededed;
  position: absolute;
}

.up {
  font-family: Gotham;
  font-style: normal;
  font-weight: 900;
  font-size: 144px;
  color: transparent;
  -webkit-text-stroke: #ededed 2px;
  z-index: 2;
  position: absolute;
}

.group {
  position: relative;
  width: 465.125px;
  height: 144px;
  left: 50%;
  right: 50%;
  transform: translate(-50%, -50%);
  margin-top: 30%;
  z-index: 1;
}
<div class="group">
  <p class="up">ZAIUS</p>
  <p class="bottom">ZAIUS</p>
</div>

我想在图像和笔触文本下面填充文本。并且笔划文本应该在另一个和图像之上。

html css z-index
3个回答
0
投票

完全不需要z-index属性来实现您的目标,您可以在下面的示例中看到。

此外,应尽可能省略z-index,以保持代码清洁,可重用,可维护和可扩展。 当然,在非常小的项目中,它并不像大型项目那么重要。

#frame {
  position: relative;
  width: 100%;
  height: 100%;
}

.group {
  position: absolute;
  width: 400px;
  height: 250px;
  left: 10%;
  top: 30%;
}

.group img {
  position: absolute;
  top: 0;
  left: 70%;
}

.big-letters {
  font-family: Gotham;
  font-style: normal;
  font-weight: 900;
  font-size: 144px;
  position: absolute;
  top: 0;
  left: 0;
}

.bottom {
  color: #ededed;
}

.up {
  color: transparent;
  -webkit-text-stroke: #ededed 2px;
}
<div id="frame">
  <div class="group">
    <div class="big-letters bottom">LOREM</div>
    <img src="https://via.placeholder.com/300x200"/>
    <div class="big-letters up">LOREM</div>
  </div>
</div>

0
投票

我不太了解z-index,但是从.up中删除z-index并将位置更改为相对工作,如果我理解的是正确的。

.bottom {
  font-family: Gotham;
  font-style: normal;
  font-weight: 900;
  font-size: 144px;
  color: #ededed;
  position: absolute;
}

.up {
  font-family: Gotham;
  font-style: normal;
  font-weight: 900;
  font-size: 144px;
  color: transparent;
  -webkit-text-stroke: #ededed 2px;
  position: relative;
}

.group {
  position: relative;
  width: 465.125px;
  height: 144px;
  left: 50%;
  right: 50%;
  transform: translate(-50%, -50%);
  margin-top: 30%;
  z-index: 1;
}
<div class="group">
  <p class="up">ZAIUS</p>
  <p class="bottom">ZAIUS</p>
</div>

0
投票

我没有在这里看到问题,只需将描边元素指定为更高的z-index。也许你会因为文字的颜色和笔画相同而感到困惑。我试着展示它HERE

© www.soinside.com 2019 - 2024. All rights reserved.