我正在开发一个web应用程序,我希望内容能够填满整个屏幕的高度。

该页面有一个标题,其中包含徽标和帐户信息。这可以是任意高度。我希望contentdiv将页面的其余部分填充到底。

我有一个header div和一个content div。目前,我正在为布局使用一个表,如下所示:

CSS和HTML

#第页{高度:100%;宽度:100%}#td含量{高度:100%;}#内容{溢出:自动;/*或溢出:隐藏*/}<table id=“page”><tr><td id=“tdheader”><div id=“header”></分区></td></tr><tr><td id=“tdcontent”><div id=“content”></分区></td></tr></table>

页面的整个高度已填充,不需要滚动。

对于contentdiv中的任何内容,设置top:0;会把它放在标题下面。有时内容将是一个真实的表格,其高度设置为100%。将标题放在内容内不允许这样做。

有没有一种方法不使用桌子就能达到同样的效果?

更新:

contentdiv中的元素也将高度设置为百分比。因此,div中100%的内容将填充到底部。50%的两个元素也是如此。

更新2:

例如,如果标题占屏幕高度的20%,则在#内容内部指定50%的表将占屏幕空间的40%。到目前为止,把整个东西包在桌子里是唯一有效的方法。


当前回答

对我来说,最简单的方法就是使用网格。但是,我正在寻找一种更简单的方法。下面是我是如何做到这一点的。但是,如果我们有很多嵌套的div,那就太痛苦了。

 <div style={{
  display:grid,
  gridTemplateRows:'max-content 1fr',
}}>
   <div>
     Header
   </div>
   <div style={{height:'100%',minHeight:'0'}}>
     Content
   </div>
 </div>

其他回答

仅限CSS进近(如果高度已知/固定)

当您希望中间元素垂直跨越整个页面时,可以使用CSS3中引入的calc()。

假设我们有一个固定高度的页眉和页脚元素,我们希望section标记采用整个可用的垂直高度。。。

Demo

假设标记和CSS应为

html,正文{高度:100%;}收割台,收割台{高度:100px;背景:灰色;}部分{高度:计算(100%-(100px+150px));/*添加100px的页眉和150px的页脚*/背景:番茄;}页脚{高度:150px;背景色:蓝色;}<header>100px</header><section>展开我的剩余空间</section><footer>150像素</footer>

所以在这里,我要做的是将元素的高度相加,然后使用calc()函数从100%中减去。

只需确保使用高度:100%;对于父元素。

实际上,您可以使用display:table将区域拆分为两个元素(页眉和内容),其中页眉的高度可以不同,而内容将填充剩余的空间。这适用于整个页面,也适用于区域仅是位置设置为相对、绝对或固定的另一个元素的内容的情况。只要父元素具有非零高度,它就可以工作。

请参见此小提琴以及以下代码:

CSS:

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

p {
    margin: 0;
    padding: 0;
}

.additional-padding {
    height: 50px;
    background-color: #DE9;
}

.as-table {
    display: table;
    height: 100%;
    width: 100%;
}

.as-table-row {
    display: table-row;
    height: 100%;
}

#content {
    width: 100%;
    height: 100%;
    background-color: #33DD44;
}

HTML格式:

<div class="as-table">
    <div id="header">
        <p>This header can vary in height, it also doesn't have to be displayed as table-row. It will simply take the necessary space and the rest below will be taken by the second div which is displayed as table-row. Now adding some copy to artificially expand the header.</p>
        <div class="additional-padding"></div>
    </div>
    <div class="as-table-row">
        <div id="content">
            <p>This is the actual content that takes the rest of the available space.</p>
        </div>
    </div>
</div>

对于移动应用程序,我只使用VH和VW

<div class="container">
    <div class="title">Title</div>
    <div class="content">Content</div>
    <div class="footer">Footer</div>
</div>
.container {
    width: 100vw;
    height: 100vh;
    font-size: 5vh;
}
    
.title {
    height: 20vh;
    background-color: red;
}
    
.content {
    height: 60vh;
    background: blue;
}
    
.footer {
    height: 20vh;
    background: green;
}

演示-https://jsfiddle.net/u763ck92/

使用flexbox的简单解决方案:

html,正文{高度:100%;}正文{显示:柔性;弯曲方向:柱;}.内容{挠曲生长:1;}<body><div>标题</div><div class=“content”></div></body>

Codepen示例

另一种解决方案,在content div中以div为中心

我也一直在寻找答案。如果您有幸能够以IE8及更高版本为目标,那么可以使用display:table和相关值来获取包含div在内的块级元素的表的呈现规则。

如果你更幸运,并且你的用户正在使用顶级浏览器(例如,如果这是你控制的计算机上的intranet应用程序,就像我的最新项目一样),你可以在CSS3中使用新的Flexible Box Layout!