html - 如何用另一个div覆盖一个div作为覆盖

标签 html css flexbox

<分区>

大家好,我想将一个 div(封面)覆盖在另一个 div(区域)上,我已经检查过与我的类似的帖子,但没有一个能满足我的特定需求。根据我的测试,它只是出现在主 div(区域)下方。容器 div(reader) 的位置是固定的,这是我能够填满整个屏幕的唯一方法。请在下面检查我的代码。谢谢

 <style>
    html,
    body {
       box-sizing: border-box;
       min-height: 100%;
       margin: 0;
       padding: 0;
    }

    #reader {
       position: fixed;
       width:100%;
       height: 100%;
       top: 10;
       left: 20;
       bottom: 10;
       right: 20;
       background: wheat;
       display: flex;
       flex-direction: column;
    }

    #reader #toolbar {
      flex: 0 1 auto;
      display: flex;
      justify-content: space-between;
      background: rgba(0,0,0,0.05);
      padding: 10px;
    }

    #reader #toolbar .left {
      flex: 0 1 auto;
    }

    #reader #toolbar .center {
      flex: 0 1 auto;
    }

    #reader #toolbar .right {
      flex: 0 1 auto;
    }

   .area {
      flex: 1 0 auto;
      display: flex;
      margin: 10px 15px;
      padding: 10px;
    }

    #reader #area div {
      position: absolute;
      width: 90%;
      top: 10px;
      left: 5%;
      bottom: 10px;
      right: 5%;
    }

    .cover {
      z-index: 9;
    }

  </style>

  <div id="reader">
     <input type="file" id="bookChooser">
     <select id="toc"></select>
     <div id="area" class="area"></div>
     <div class="area cover"></div> <!-- This should cover the div area above, not pushed down -->
     <button id="prev" type="button"><</button>
     <button id="next" type="button">></button>
  </div>

最佳答案

将两个 div 放在根 div 中。然后设置根 div position:relative 并覆盖 div absolute。固定高度和宽度。并将 display:block 应用到 overlay div 上。如果仍然无效,则应用 z-index。

这应该是这样的: HTML:

<div class="parent">
     <div id="area" class="area"></div>
     <div class="area cover"></div>
</div>

CSS:

.parent{
  position: relative;
  height:200px;
  width: 200px;
}
.cover{
  position: absolute;
  height: 100%;
  width: 100%;
  display: block;
  z-index: 999;
}

希望这对你有用。

关于html - 如何用另一个div覆盖一个div作为覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67138066/

相关文章:

html - CSS3 动画中元素的旋转不平滑和连续

html - 媒体查询不适用于移动设备

html - Flexbox 粘性页脚和溢出的内容区域

css - 为什么我的 flex-box 元素 "float"在我将它们的元素设置为彼此堆叠( block )时彼此相邻?

html - 文字间距大,如何解决?

html - 绝对定位让页脚消失

javascript - 相同的选择器单击添加和删除

CSS - 在屏幕上移动网格元素调整大小

CSS 宽度未按预期相加

html - 调整由 justify-content space-between 设置的元素之间的间距