【问题标题】:How to make DIV take the full height of Browser如何让DIV占据浏览器的全高
【发布时间】:2021-10-27 20:03:13
【问题描述】:

我想制作一个高度为 100% 的 DIV,以占据 浏览器窗口的全部高度,而不仅仅是视口高度。

外部 DIV 具有 Display 属性 Flex

目标 DIV 的类名为 full-height-div

那是HTML代码

<div class="main-container">
    <div class="full-height-div">
        
    </div>
    <div class="main-content">
        
    </div>
</div>

那是 CSS 代码

body, html {
    margin: 0;
    height: 100%;
}

.main-container {
    display: flex;
    height: 100%;
}

.full-height-div {
    height: 100%;
}

【问题讨论】:

标签: javascript html jquery css web


【解决方案1】:

听起来您希望&lt;div class="full-height-div"&gt; 成为其父级的高度。

.main-container 需要flex-direction: row,而不是height: 100%

在此示例中,&lt;div class="full-height-div"&gt;(红色轮廓)采用其父 div.main-container(绿色轮廓)的高度。

body, html {
    margin: 0;
    height: 100%;
}

.main-container {
    display: flex;
    flex-direction: row;    
    border: 5px solid green;
    width: 100%;
}

.full-height-div {
    height: 2000px; /* just to force it to be taller than the viewport in this example */
    border: 1px solid red;
    width: 100%;
}
<div class="main-container">
    <div class="full-height-div">
    </div>
    <div class="main-content">
    </div>
</div>

此处已回答:How can I make Flexbox children 100% height of their parent?

【讨论】:

    【解决方案2】:

    body, html {
        margin: 0;
        height: 100%;
    }
    
    .full-height-div {
        height: 100vh;
        background-color:blue;
    }
    
    .main-container {
        display: flex;
        background-color:red;
    }
    <div class="main-container">
        <div class="full-height-div">
            full
        </div>
        <div class="main-content">
            main
        </div>
    </div>
    .full-height-div {
        height: 100vh;
    }
    

    【讨论】:

    • 不行,我希望 DIV 是浏览器的全高而不是视口
    • 如果主内容 DIV 的高度大于视口,则它不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 2017-11-30
    • 2011-01-02
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多