如何扩展div以覆盖剩余页面高度
抱歉,搜索返回了大量结果,但没有任何结果完全符合我的问题。看起来 SO 陷入了 div 高度问题。 :-(
我有以下测试布局:
<!DOCTYPE html>
<html>
<head>
<title>Div Test</title>
<style type="text/css" media="screen">
body {
margin:0;
}
div {
width:100%;
}
.content {
position:relative;
float:none;
margin-left:auto;
margin-right:auto;
display:block;
width:680px;
}
#one {
height:100px;
position:relative;
background-color:#0000FF;
}
#two {
position:relative;
background-color:#00FF00;
height:100%;
min-height:400px;
}
#three {
height:70px;
background-color:#FF0000;
bottom:0;
position:absolute;
}
#oneone {
height:100%;
background-color:#838383;
}
#twotwo {
height:400px;
background-color:#EEFF47;
}
#threethree {
height:100%;
background-color:#400080;
}
</style>
</head>
<body>
<div id="one">
<div class="content" id="oneone"></div>
</div>
<div id="two">
<div class="content" id="twotwo">Expand this to the bottom.</div>
</div>
<div id="three">
<div class="content" id="threethree"></div>
</div>
</body>
</html>
我希望 div 1 和 div 3 保持在顶部和底部,div Twotwo 应该扩展高度以适应我的页面内容,并在内容超过页面高度时扩展,因此推动div 向下三个并导致滚动。
如果可以,CSS 解决方案是什么? 谢谢!
Sorry, searching has returned tons of results, but nothing that matches my problem exactly. Seems like SO is drowning in div-height problems. :-(
I have the following test layout:
<!DOCTYPE html>
<html>
<head>
<title>Div Test</title>
<style type="text/css" media="screen">
body {
margin:0;
}
div {
width:100%;
}
.content {
position:relative;
float:none;
margin-left:auto;
margin-right:auto;
display:block;
width:680px;
}
#one {
height:100px;
position:relative;
background-color:#0000FF;
}
#two {
position:relative;
background-color:#00FF00;
height:100%;
min-height:400px;
}
#three {
height:70px;
background-color:#FF0000;
bottom:0;
position:absolute;
}
#oneone {
height:100%;
background-color:#838383;
}
#twotwo {
height:400px;
background-color:#EEFF47;
}
#threethree {
height:100%;
background-color:#400080;
}
</style>
</head>
<body>
<div id="one">
<div class="content" id="oneone"></div>
</div>
<div id="two">
<div class="content" id="twotwo">Expand this to the bottom.</div>
</div>
<div id="three">
<div class="content" id="threethree"></div>
</div>
</body>
</html>
I want the div one and three to stay in top and bottom, div twotwo should expand in height to accommodate my pagecontent, and expand when the content is more than the page height, therefore pushing the div three down and cause scrolling.
Is this possible without JavaScript? If so, what's the CSS solution to this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此需要 JavaScript。当您有页眉和页脚高度并且希望中间具有 100% 高度时,结果将是整页高度 + 页眉高度 + 页脚高度。因此,您最终将使用滚动条来查看整个页面。如果您希望所有内容都适合在同一个窗口中,那么我们需要使用 javascript 来检测剩余的中间空间并相应地分配高度。
要使用 jQuery 检测中间高度,您可以执行
此操作,这将为您提供中间部分的剩余高度,在您的情况下为
。
请参阅 http://jsfiddle.net/Ppw5y/ 中的 jQuery 代码。请注意,当您展开窗口并再次运行代码时,您将获得不同的内容高度。
Javascript is needed for this. When you have a header and footer height and you want the middle to have 100% height, the result will be full page height + header height + footer height. So you'll end up with scroll bars to view the entire page. If you want everything to fit in the same window, then you we need to use javascript to detect how much middle space is left and assign height accordingly.
To detect middle height with jQuery, you can do
This will give you the height that is left for the middle part which is
<div id="two">
in your case.See jQuery code in action at http://jsfiddle.net/Ppw5y/. Notice that when you expand the window and you run the code again, you will have a different content height.
如何设置 height:auto;对于#two 和#twotwo?
How about setting height:auto; for #two and #twotwo?
我认为无需脚本即可完成。检查这个代码。
我在更改代码的地方添加了 /* 已更改 */ 。
I think it can be done without script. Check this code out.
I have added a /* changed */ wherever i have changed your code.