【问题标题】:100% height div between header and footer页眉和页脚之间的 100% 高度 div
【发布时间】:2013-04-25 17:08:39
【问题描述】:

我正在尝试创建一个网页布局,其中包含页眉/页脚(100% 宽度,145 像素高度)、页眉/页脚(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;
}

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

谢谢!

【问题讨论】:

    标签: html css frontend


    【解决方案1】:

    小提琴: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 伪元素而不是分隔 div 的替代方法: http://jsfiddle.net/gBr58/1/

    虽然在 IE7 或 6 中无法使用,但要在 IE8 中使用,必须声明 &lt;!DOCTYPE&gt;(根据 w3schools.com),但 HTML 干净整洁

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

    已将其更新到 IE6。我通常不会打扰,因为我的公司不支持 IE6,但这很容易解决...

    【讨论】:

    • 看来你已经破解了!
    【解决方案2】:

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

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

    【讨论】:

    • 页脚不应始终位于可见窗口的底部,但是(例如固定页脚)它应该仅在内容最少的情况下位于屏幕底部,然后调整为位于下方当内容本身填满屏幕时的所有内容。基本上 CSS Sticky Footer 是如何工作的,只有内容周围的容器必须填充页眉和页脚之间的整个高度(这是复杂之处)。
    • 啊,我明白了。听起来有点复杂,需要 JS 解决方案。
    • 关于如何使用 JS/Jquery 完成此任务的任何建议?
    猜你喜欢
    • 2019-08-11
    • 2012-04-30
    • 2015-08-17
    • 2018-05-17
    • 2019-05-05
    • 2012-07-07
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    相关资源
    最近更新 更多