将div放在相对于文档的顶部屏幕上

时间:2017-03-06 16:07:18

标签: html css html5 css3 sass

我有一个结构,我无法改变一些元素。 意思是模态位于顶部,而不改变div的位置。 (或者至少不超过方框3)

我可以改变的元素是box3,box4和modal。

我希望黄色方块在整个文档上重叠,现在它被卡在其父文件中(请参阅代码段或jsFiddle) 有没有办法做到这一点?

CSS& HTML:



.modal {
  position: absolute;
  top: 0;
  left: 0;
  height: 40px;
  width: 40px;
  background-color: yellow;
}

.box4 {
  display: block;
  height: 150px;
}

.box3 {
  display: block;
  position: relative;
  margin: 0 auto;
  padding-right: 18px;
  padding-left: 18px;
  clear: both;
  border: 2px solid purple;
  height: 150px;
}

.box2 {
  border: 2px solid green;
  padding-right: 10px;
  padding-left: 10px;
  width: 100%;
  position: relative;
  float: left;
  display: block;
  box-sizing: border-box;
  min-height: 200px;
}

.upperRow {
  display: block;
  box-sizing: border-box;
  height: 20px;
}

.box1 {
  position: relative;
  width: 100%;
  height: 100%;
  background: #fff;
  -webkit-transform-style: preserve-3d;
  z-index: 2;
  border: 2px solid black;
}

header {
  position: relative;
  background-color: red;
  height: 50px;
  display: block;
  box-sizing: border-box;
}

footer {
  height: 50px;
  padding-top: 8px;
  margin-top: 8px;
  float: left;
  width: 100%;
  background-color: blue;
  display: block;
}

<!-- HTML -->

<header>

</header>
<div class="box1">
  <section class="upperRow">

  </section>
  <section class="box2">
    <div class="box3">
      <!-- box3 CAN BE MODIFIED-->
      <div class="box4">
        <!-- box4 CAN BE MODIFIED-->

      </div>
      <div class="modal">
        <!-- modal CAN BE MODIFIED-->
      </div>
    </div>
  </section>
</div>
<footer>

</footer>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

这是因为父母有position: relative;-webkit-transform-style: preserve-3d;。如果删除它,那很好。

&#13;
&#13;
.modal {
  position: absolute;
  top: 0;
  left: 0;
  height: 40px;
  width: 40px;
  background-color: yellow;
}

.box4 {
  display: block;
  height: 150px;
}

.box3 {
  display: block;
  margin: 0 auto;
  padding-right: 18px;
  padding-left: 18px;
  clear: both;
  border: 2px solid purple;
  height: 150px;
}

.box2 {
  border: 2px solid green;
  padding-right: 10px;
  padding-left: 10px;
  width: 100%;
  float: left;
  display: block;
  box-sizing: border-box;
  min-height: 200px;
}

.upperRow {
  display: block;
  box-sizing: border-box;
  height: 20px;
}

.box1 {
  width: 100%;
  height: 100%;
  background: #fff;
  z-index: 2;
  border: 2px solid black;
}

header {
  position: relative;
  background-color: red;
  height: 50px;
  display: block;
  box-sizing: border-box;
}

footer {
  height: 50px;
  padding-top: 8px;
  margin-top: 8px;
  float: left;
  width: 100%;
  background-color: blue;
  display: block;
}
&#13;
<!-- HTML -->

<header>

</header>
<div class="box1">
  <section class="upperRow">

  </section>
  <section class="box2">
    <div class="box3">
      <!-- box3 CAN BE MODIFIED-->
      <div class="box4">
        <!-- box4 CAN BE MODIFIED-->

      </div>
      <div class="modal">
        <!-- modal CAN BE MODIFIED-->
      </div>
    </div>
  </section>
</div>
<footer>

</footer>
&#13;
&#13;
&#13;