我正在开发一个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%。到目前为止,把整个东西包在桌子里是唯一有效的方法。


当前回答

这是一个使用网格的答案。

.the-container-div {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto min-content;
  height: 100vh;
}
.view-to-remain-small {
  grid-row: 2;
}

.view-to-be-stretched {
  grid-row: 1
}

其他回答

您可以使用CSS表,而不是在标记中使用表。

加成

<body>    
    <div>hello </div>
    <div>there</div>
</body>

(相关)CSS

body
{
    display:table;
    width:100%;
}
div
{
    display:table-row;
}
div+ div
{
    height:100%;  
}

FIDDLE1和FIDDLE2

该方法的一些优点是:

1) 较少的加价

2) 标记比表更具语义,因为这不是表格数据。

3) 浏览器支持非常好:IE8+,所有现代浏览器和移动设备(犬舍)

为了完整起见,下面是css表模型中与css财产等效的Html元素

table    { display: table }
tr       { display: table-row }
thead    { display: table-header-group }
tbody    { display: table-row-group }
tfoot    { display: table-footer-group }
col      { display: table-column }
colgroup { display: table-column-group }
td, th   { display: table-cell }
caption  { display: table-caption } 

实际上,您可以使用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>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<style type="text/css">
body
,html
{
    height: 100%;
    margin: 0;
    padding: 0;
    color: #FFF;
}

#header
{
    float: left;
    width: 100%;
    background: red;
}

#content
{
    height: 100%;
    overflow: auto;
    background: blue;
}

</style>
</head>
<body>

    <div id="content">
        <div id="header">
                Header
                <p>Header stuff</p>
        </div>
            Content
            <p>Content stuff</p>
    </div>

</body>
</html>

在所有正常的浏览器中,您可以将“header”div放在内容之前,作为同级,同样的CSS也可以工作。然而,如果浮点数为100%,IE7-无法正确解释高度,因此标头需要在内容中,如上所述。溢出:auto将导致IE上的双滚动条(它始终显示视口滚动条,但禁用),但如果没有它,如果溢出,内容将被剪切。

使用CSS网格的另一个解决方案

定义网格

.root {
  display: grid;
  grid-template-rows: minmax(60px, auto) minmax(0, 100%);
}

第一行(标题):可以设置最小高度,最大高度取决于内容。第二行(内容)将尝试容纳标题后剩余的可用空间。

这种方法的优点是内容可以独立于页眉滚动,因此页眉始终位于页面顶部

正文,html{边距:0;高度:100%;}.根{显示:栅格;网格模板行:minmax(60px,自动)minmax(0,100%);高度:100%;}.页眉{背景色:浅蓝色;}按钮{背景色:深色板岩;颜色:白色;填充:10px 50px;边距:10px 30px;边框半径:15px;边框:无;}.内容{背景色:仿古白;溢出:自动;}.块{宽度:计算(100%-20px);高度:120px;边框:实心海蓝宝石;边距:10px;}<div class=“root”><div class=“header”><button>单击</button><button>单击</button><button>单击</button><button>单击</button><button>单击</button></div><div class=“content”><div class=“block”></div><div class=“block”></div><div class=“block”></div><div class=“block”></div><div class=“block”></div><div class=“block”></div><div class=“block”></div><div class=“block”></div></div><div class=“footer”></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/