简体   繁体   English

如何让 div 填充剩余空间?

[英]How do I make div fill the remaining space?

Let's say I have this basic html layout:假设我有这个基本的 html 布局:

  • Header标题

  • Auto-sizing DIV自动调整大小的 DIV

  • Footer.页脚。

Header and footer have fixed pixel size.页眉和页脚具有固定的像素大小。

<div id="header">
Lorem Ipsum
</div>

<div id="auto">
This div should automatically change size
</div>

</div id="footer">
Lorem Ipsum
</div>

And the CSS和 CSS

document,body {
width:100%;
height:100%;
}
#header {
width:100%;
height:50px;
}
#auto {
height:100% (it fills 100% of the window height instead the remaining)
}
#footer {
width:100%;
height:50px;
}

How do I make div fill the remaining space?如何让 div 填充剩余空间?

There are many ways to do this, but I myself use a sticky footer.有很多方法可以做到这一点,但我自己使用粘性页脚。

CSS-Tricks CSS 技巧
StickyFooter粘性页脚

I'd advise you to create a container div element which contains the header , main and footer and set that with width:100%; height:100% min-height:???;我建议您创建一个容器div元素,其中包含headermainfooter并将其设置为width:100%; height:100% min-height:???; width:100%; height:100% min-height:???;

You could fix it by getting the window height with jQuery and take the height of the header and footer of the windows height and set that on your div.您可以通过使用 jQuery 获取窗口高度并获取窗口高度的页眉和页脚的高度并将其设置在您的 div 上来修复它。

Like this:像这样:

header {
    background: red;
    height: 10px;
    position: fixed;
    top: 0;
    width:100%;
}

.resizable {
    background: blue;
    width: 100%;
}

footer {
    background: red;
    bottom: 0;    
    height: 10px;
    position: fixed;
    width:100%;
}

And this is the jQuery:这是 jQuery:

$(document).ready(function(){

    var newHeight = $(window).height() - 20;

    $(".resizable").css("height", newHeight);

});

Working JSFiddle工作 JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM