【问题标题】:Set div to 100% height of parent even when viewport is larger than parent [duplicate]即使视口大于父级,也将 div 设置为父级的 100% 高度 [重复]
【发布时间】:2020-11-05 23:05:38
【问题描述】:

我想将“背景”div 的高度设置为其父级的 100%,即使它大于视口,如

<!DOCTYPE html>
<html style="height:100%; margin:0; padding:0">
  <body style="height:100%; margin:0; padding:0">

    <div style="height:2000px; margin:0; padding:0; background-color:red">
      // Background div.
      <div style=" position:absolute; top:0; height:100%; width:100%; min-width:100%; background-color:blue">
      </div>

      // More divs with content on top.

    </div>
  </body>
</html>

这应该很简单,但我还没有找到任何方法(在 Firefox 和 Chrome 中测试过)?

【问题讨论】:

    标签: html css height viewport


    【解决方案1】:

    你应该使用“位置:相对;”对于父 div, 测试一下:

    <!DOCTYPE html>
    <html style="height:100%; margin:0; padding:0">
      <body style="height:100%; margin:0; padding:0">
    
        <div style="position:relative; height:2000px; margin:0; padding:0; background-color:red">
          // Background div.
          <div style=" position:absolute; top:0; height:100%; width:100%; min-width:100%; background-color:blue">
          </div>
    
          // More divs with content on top.
    
        </div>
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      相关资源
      最近更新 更多