【问题标题】:How to cover a div with another div as overlay [duplicate]如何用另一个div覆盖一个div作为覆盖[重复]
【发布时间】:2021-07-12 05:42:24
【问题描述】:

大家好,我想将一个 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>

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    在根 div 中取两个 div。然后设置根 div position:relative 和覆盖 div 绝对值。固定高度和宽度。并在覆盖 div 上应用 display:bloCK。如果仍然不起作用,则应用 z-index。

    这应该是这样的: HTML:

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

    CSS:

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

    希望这对你有用。

    【讨论】:

    • 感谢@Sudipta Das 的回复,实际上父 div 必须是 100% 的宽度和高度,我会尝试您的代码并回复您。谢谢
    【解决方案2】:

    M8,

    位置固定 - 将元素固定到屏幕视图。 如果你想覆盖例如div1 by div2 您应该将 div2 INTO div1,而不是将位置 od div1 设置为相对位置,这将允许您将位置绝对设置为 div2 并且它将高于它。如果您将某些内容设置为绝对,则需要有一些引用者,该引用者是具有相对位置的父元素,或者如果存在任何相对父元素,则最后是 BODY。

    另一种解决方案是:如果您的 div1 已知高度,您可以在 DIV2 上使用 margin-top (minus) -HEIGHTofDIV1 px 将它移动到顶部并覆盖 div1。

    【讨论】:

    • 感谢@szymanskicode 的回复,我会尝试并回复您。
    【解决方案3】:

    你可以让 'cover' 成为 'area' 的孩子

    <div id="area" class="area">
      <div class="cover"></div>
    </div>
    
    .area {
      position: relative;
      flex: 1 0 auto;
      margin: 10px 15px;
      padding: 10px;
    }
    
    .area .cover {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      background: #000;
      opacity:0.5;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2014-01-26
      • 2011-02-25
      • 2011-10-06
      相关资源
      最近更新 更多