我想从box1后面显示我的box2。

我可以使用position: relative;进入.box1

但是,当我在项目中使用该position时,我遇到了另一个问题。

如何在不将.box2设置为.box1的情况下从position: relative;后面显示.box1



.main {
  height: 100%;
}

#box1 {
  background: red;
  height: 200px;
  width: 100px;
  z-index: 999;
}

#box2 {
  position: relative;
  padding-top: 100px;
  padding-bottom: 79px;
  color: white;
  margin-top: -200px;
  margin-left: 110px;
  background: black;
  heigth: 200px;
  width: 100px;
  z-index: 1;
}

.animated {
  animation-duration: 1s;
  animation-fill-mode: both;
}

@keyframes slideInLeft {
  from {
    transform: translate3d(-100%, 0, 0);
    visibility: visible;
  }

  to {
    transform: translate3d(0, 0, 0);
  }
}
.slideInLeft {
  animation-name: slideInLeft;
}

<div class="main">
<div id="box1">
</div>
<div id="box2" class="animated slideInLeft">
Some text
</div>

</div>

最佳答案

将z-index更改为-1



.main {
  height: 100%;
}

#box1 {
  background: red;
  height: 200px;
  width: 100px;
  z-index: 999;
}

#box2 {
  position: relative;
  margin: auto;
  color: white;
  margin-top: -200px;
  margin-left: 110px;
  background: black;
  height: 200px;
  width: 100px;
  z-index: -1;
}

.animated {
  animation-duration: 1s;
  animation-fill-mode: both;
}

@keyframes slideInLeft {
  from {
    transform: translate3d(-100%, 0, 0);
    visibility: visible;
  }
  to {
    transform: translate3d(0, 0, 0);
  }
}

.slideInLeft {
  animation-name: slideInLeft;
}

<div class="main">

  <div id="box1">
  </div>
  <div id="box2" class="animated slideInLeft">
    Some text
  </div>

</div>

关于html - 从框1后面显示框2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47786401/

10-09 14:35