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


当前回答

试试这个

var sizeFooter = function(){
    $(".webfooter")
        .css("padding-bottom", "0px")
        .css("padding-bottom", $(window).height() - $("body").height())
}
$(window).resize(sizeFooter);

其他回答

原来的帖子是3年多前的。我想很多像我一样来这篇文章的人都在寻找一个类似于应用程序的布局解决方案,比如固定的页眉、页脚和占据其余屏幕的全高内容。如果是这样,这篇文章可能会有所帮助,它可以在IE7+等平台上运行。

http://blog.stevensanderson.com/2011/10/05/full-height-app-layouts-a-css-trick-to-make-it-easier/

下面是这篇文章的一些片段:

@媒体屏幕{/*屏幕开始规则。*//*通用窗格规则*/正文{边距:0}.row,.col{溢出:隐藏;位置:绝对;}.行{左:0;右:0;}.col{顶部:0;底部:0;}.soll-x{overflow-x:auto;}.soll-y{overflow-y:auto;}.header.row{height:75px;top:0;}.body.row{顶部:75px;底部:50px;}.footer.row{高度:50px;底部:0;}/*屏幕结束规则。*/}<div class=“header-row”style=“background:yellow;”><h2>我的页眉</h2></div><div class=“body row scroll-y”style=“background:lightblue;”><p>身体</p></div><div class=“footer row”style=“background:#e9e9e9;”>我的页脚</div>

它从未像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>

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

为什么不这样呢?

html, body {
    height: 100%;
}

#containerInput {
    background-image: url('../img/edit_bg.jpg');
    height: 40%;
}

#containerControl {
    background-image: url('../img/control_bg.jpg');
    height: 60%;
}

给html和body(按顺序)一个高度,然后给元素一个高度?

为我工作

我的一些组件是动态加载的,这给我设置导航栏的高度带来了问题。

我所做的是使用ResizeObserver API。

function observeMainResize(){
   const resizeObserver = new ResizeObserver(entries => {
      for (let entry of entries) {
         $("nav").height(Math.max($("main").height(),
                                  $("nav") .height()));
      }
   });
   resizeObserver.observe(document.querySelector('main'));
}

那么:

...
<body onload="observeMainResize()">
   <nav>...</nav>
   <main>...</main>
...