如何将div高度扩展到窗口允许的高度?

时间:2014-09-04 03:38:50

标签: html css sticky-footer

我有一个带有固定标题和粘性页脚的公共页面structure。但我无法绕过如何扩展div高度以填满整个窗口区域。

HTML

<header>
    header header header
</header>

<div id="page">
    <div id="left">side bar side bar</div>
    <div id="right">
    <p>I want to draw the brown dashed line all the way down to the footer even when the #right content is too little to fill the window.</p>
    <p>I know that I have to set height 100% on #right so that it stretches to fill the green box. But, the green box itself does not stretch to the bottom despite height 100% because the yellow box does not have explicit height defined.</p>
    <p>What can I do?</p>
    </div>
</div>

<footer>
    footer footer footer
</footer>

CSS

html, body, foot, header, div { padding: 0; margin: 0; box-sizing: border-box; }
p { margin-top: 0 }
html { height: 100%; min-height: 100%; }
header { position: fixed; background-color: red; height: 50px; width: 100%; }
footer { position: absolute; bottom: 0; width: 100%; background-color: cyan; }
body {
    position:relative; /* container for footer */
    border: 5px solid yellow;
    min-height: 100%; /* not explicitly setting height to allow footer to be pushed downwards */
}
#page {
    padding: 60px 0 20px 0;
    border: 5px solid green;
    height: 100%; /* not working: how to extend page all the way to the bottom, min-height = fill the window? */
}
#page:after { content:""; display: block; clear:both; }
#left { float: left; width: 100px; }
#right {
    margin-left: 100px;
    padding-left: 10px;
    /* objective: to create vertical divider between #right and #left that extends to the footer */
    height: 100%;
    border-left: 5px dashed brown;
}

2 个答案:

答案 0 :(得分:0)

您可以使用绝对定位使div始终与窗口的顶部和底部相距0px。您可能需要使用值,但这样的事情应该起作用:

#left { position: absolute; top: 0; bottom: 0; left: 0; width: 20%; }
#right { position: absolute; top: 0; bottom: 0; right: 0; width: 80%; }

修改:Here's a fiddle,说明这是如何运作的。

答案 1 :(得分:0)

好吧,高度100%不能正常工作的原因是因为身体根本没有高度,它的高度取决于身体内的物品。

有一个解决方法

将以下内容应用于您的样式。

html, html body {
    height: 100%;
}
#page { /* If #page is a lvl 1 child of body, this should work */
    height: 100%;
}

这是JSFiddle

http://jsfiddle.net/wetjyLy3/1/