页眉和页脚之间的高度为100%

时间:2013-05-01 16:51:03

标签: html css html5 css3 frontend

我正在尝试使用页眉/页脚(100%宽度,145px高度)创建网页布局,页眉/页脚之间的“主要区域”(100%宽度,动态高度)以及内容周围的容器这是一种独特的背景颜色(宽度为860像素,动态高度,但总是与页脚“齐平”)。

See Example for a visual

我遇到的问题是,当内容很少时,我似乎无法将“内容容器”与页脚齐平。使用original example之类的设置会导致页脚浮动内容,如果存在可观/“正常”的内容量或者窗口已调整大小。

以下CSS导致内容和页脚之间存在差距。

html,body{
   margin:0;
   padding:0;
   height:100%;
  background:yellow;
}

.wrap{
   min-height:100%;
   position:relative;
}

header{
  background:blue;
   padding:10px;  
}

#content{
  height:100%;
  width: 400px;
  margin:0 auto;
  background:orange;
    padding:30px;
}
footer{
  background:blue;
  position:absolute;
  bottom:0;
  width:100%;
  height:60px;
}

如果内容最小并且页脚“粘贴”到页面底部,我怎样才能使内容容器成为屏幕的整个高度,同时如果内容正常,还可以动态调整大小(页脚总是在内容的底部)?

谢谢!

2 个答案:

答案 0 :(得分:12)

FIDDLE:http://jsfiddle.net/3R6TZ/2/

小提琴输出:http://fiddle.jshell.net/3R6TZ/2/show/

<强> CSS

html, body {
    height: 100%;
    margin:0;
}
body {
    background:yellow; 
}
#wrapper {
    position: relative;
    min-height: 100%;
    vertical-align:bottom;
    margin:0 auto;
    height:100%;
}
#header {
    width: 100%;
    height: 150px;
    background:blue;
    position:absolute;
    left:0;
    top:0;
}
#content {
    background:pink;
    width:400px;
    margin:0 auto -30px;
    min-height:100%;
    height:auto !important;
    height:100%;
}
#content-spacer-top {
    height:150px;
}
#content-spacer-bottom {
    height:30px;
}
#divFooter {
    width:100%;
    height: 30px;
    background:blue;
}

<强> HTML

<div id="wrapper">
    <div id="header">Header</div>
    <div id="content">
        <div id="content-spacer-top"></div>
        <div id="content-inner">
            **Content Goes Here**
        </div>
        <div id="content-spacer-bottom"></div>
    </div>
    <div id="divFooter">Footer</div>
</div>

<强>更新

#content-spacer-top#content-spacer-bottom用于填充#content div而不使用填充或边距,这会增加超过100%高度的框尺寸,从而导致问题。

在CSS3中,有box-sizing属性(more info here)可以解决这个问题,但我假设您不想依赖CSS3功能。

修改

添加了修复并测试了IE7

更新2

使用:before:after伪元素而不是spacer div的替代方法: http://jsfiddle.net/gBr58/1/

但是在IE7或6中不起作用,并且要在IE8中工作,必须声明<!DOCTYPE>(根据w3schools.com),但HTML很干净

更新3 (抱歉有这么多更新)

更新它以适应IE6。我通常不会打扰,因为我的公司不支持IE6,但这是一个简单的修复......

答案 1 :(得分:0)

我认为你需要位置:固定在页脚上:

footer {
    width: 100%;
    height: 30px;
    position:fixed;
    bottom:0;
}