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


当前回答

您可以使用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 } 

其他回答

在CSS中,确实没有一种可靠的跨浏览器方式来实现这一点。假设您的布局很复杂,您需要使用JavaScript来设置元素的高度。你需要做的本质是:

Element Height = Viewport height - element.offset.top - desired bottom margin

一旦您可以获得该值并设置元素的高度,就需要将事件处理程序附加到窗口onload和onresize,以便可以启动调整大小函数。

此外,假设内容可能大于视口,则需要将overflow-y设置为滚动。

它从未像NICCAI在第一个答案中建议的那样,以其他方式对我起作用。我正在使用这种方法用谷歌地图重新缩放<div>。

以下是如何做到这一点的完整示例(适用于Safari/FireFox/IE/iPhone/Androrid(适用于旋转)):

CSS

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

.header {
  height: 100px;
  background-color: red;
}

.content {
  height: 100%;
  background-color: green;
}

JS

function resize() {
  // Get elements and necessary element heights
  var contentDiv = document.getElementById("contentId");
  var headerDiv = document.getElementById("headerId");
  var headerHeight = headerDiv.offsetHeight;

  // Get view height
  var viewportHeight = document.getElementsByTagName('body')[0].clientHeight;

  // Compute the content height - we want to fill the whole remaining area
  // in browser window
  contentDiv.style.height = viewportHeight - headerHeight;
}

window.onload = resize;
window.onresize = resize;

HTML

<body>
  <div class="header" id="headerId">Hello</div>
  <div class="content" id="contentId"></div>
</body>

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

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

我也有同样的问题,但我无法用上面的柔性箱解决问题。因此,我创建了自己的模板,其中包括:

具有固定大小元素的标头页脚带有滚动条的侧栏,占据剩余高度所容纳之物

我使用了flexbox,但方法更简单,只使用了财产display:flex和flex-direction:row|column:

我确实使用了angular,我希望我的组件大小是它们的父元素的100%。

关键是为所有父母设置大小(以百分比为单位),以限制他们的大小。在以下示例中,myapp高度占视口的100%。

主组件占视口的90%,因为页眉和页脚占5%。

我在这里发布了我的模板:https://jsfiddle.net/abreneliere/mrjh6y2e/3

       body{
        margin: 0;
        color: white;
        height: 100%;
    }
    div#myapp
    {
        display: flex;
        flex-direction: column;
        background-color: red; /* <-- painful color for your eyes ! */
        height: 100%; /* <-- if you remove this line, myapp has no limited height */
    }
    div#main /* parent div for sidebar and content */
    {
        display: flex;
        width: 100%;
        height: 90%; 
    }
    div#header {
        background-color: #333;
        height: 5%;
    }
    div#footer {
        background-color: #222;
        height: 5%;
    }
    div#sidebar {
        background-color: #666;
        width: 20%;
        overflow-y: auto;
     }
    div#content {
        background-color: #888;
        width: 80%;
        overflow-y: auto;
    }
    div.fized_size_element {
        background-color: #AAA;
        display: block;
        width: 100px;
        height: 50px;
        margin: 5px;
    }

Html:

<body>
<div id="myapp">
    <div id="header">
        HEADER
        <div class="fized_size_element"></div>

    </div>
    <div id="main">
        <div id="sidebar">
            SIDEBAR
            <div class="fized_size_element"></div>
            <div class="fized_size_element"></div>
            <div class="fized_size_element"></div>
            <div class="fized_size_element"></div>
            <div class="fized_size_element"></div>
            <div class="fized_size_element"></div>
            <div class="fized_size_element"></div>
            <div class="fized_size_element"></div>
        </div>
        <div id="content">
            CONTENT
        </div>
    </div>
    <div id="footer">
        FOOTER
    </div>
</div>
</body>

仅限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%;对于父元素。