从方框1后面显示方框2

时间:2017-12-13 05:51:19

标签: html css

我想从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>

1 个答案:

答案 0 :(得分:0)

将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>