css - 3行布局,中间展开,最小高度:100% so footer is at bottom when there is minimal content

标签 css

我该如何更改它以使中间的 div 垂直扩展以填充空白区域?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">

body,td,th {
    font-family: Tahoma, Geneva, Verdana, sans-serif;
}

html,body {
    margin:0;
    padding:0;
    height:100%; /* needed for container min-height */
}

#container {
    position:relative; /* needed for footer positioning*/
    margin:0 auto; /* center, not in IE5 */
    width:100%;

    height:auto !important; /* real browsers */
    height:100%; /* IE6: treaded as min-height*/

    min-height:100%; /* real browsers */
}

#header {
    height: 150px;
    border-bottom: 2px solid #ff8800;
    position: relative;
    background-color: #c97c3e;
}

#middle {
    padding-right: 90px;
    padding-left: 90px;
    padding-top: 35px;
    padding-bottom: 43px;
    background-color: #0F9;
}
#footer {
    border-top: 2px solid #ff8800;
    background-color: #ffd376;
    position:absolute;
    width:100%;
    bottom:0; /* stick to bottom */
}
</style>
</head>

<body>

<div id="container">
    <div id="header">
        Header
    </div>
    <div id="middle">
        Middle
    </div>
    <div id="footer">
        Footer
    </div>
</div>

</body>
</html>

最佳答案

如果没有 Javascript,您无法让实际的 div 展开以填充空白,但您可以轻松地让它看起来像这样做。移动你的规则 background-color:#0F9;从#middle 到#container。这将为您提供所需的行为(当内容最少时它会填补空白,当内容很多时它会向下扩展并插入页脚)。

但是,如果您需要 Javascript 解决方案,则可以使用以下代码。只需将其放入您的 HTML head 部分:

<script type="text/javascript">
window.onload = function() {
    var mid = document.getElementById('middle');
    var foot = document.getElementById('footer');
    mid.style.height = ((foot.offsetTop+foot.clientHeight)-(mid.offsetTop+mid.clientHeight))+'px';
};
</script>

关于css - 3行布局,中间展开,最小高度:100% so footer is at bottom when there is minimal content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2839582/

相关文章:

html - 将文本定位在其包含的 div 的底部

制作网站菜单的 HTML/CSS

javascript - .js 和 .css 的语法错误 - ASP.NET MVC 5

jQuery 类过滤和 css 转换延迟/阻塞动画在下面的部分

css - Twitter bootstrap 可更改的垂直/水平形式布局,只需少量修改

javascript - 通过 JQuery 添加 CSS 不适用于 IE

css - Bootstrap 的奇怪行为

javascript - Bootstrap 模态可点击 div

jquery - 如何通过jquery按Tab键迭代菜单的每个子菜单项?

css - 使用一个 css 规则指定顶部和底部边框