如何使Bootstrap粘性页脚内容达到整页高度?

时间:2013-07-16 14:40:39

标签: html css layout twitter-bootstrap sticky-footer

我正在使用Boostrap示例使用CSS为网站创建一个粘性页脚,这一切都很好,页面调整大小后,页脚底部保留在页面底部。 在许多页面上,我有内容需要显示几乎整页,禁止页脚。因此,页面的内容部分需要设置为100%高度,因此其内容可以调整为全高。

Here's a JSFiddle that demonstrates the problem.

我们怎样才能让绿色容器达到全高,所以它触及顶部页面顶部和页脚底部的顶部?

谢谢!

<div id="wrap">
    <!-- Begin page content -->
    <div class="container">
        <div class="page-header">
            <h1>Sticky footer</h1>

        </div>
        <p class="lead">This is the sticky footer template taken from the Bootstrap web site.</p>
        <p class="lead">How can we make this green .container div the full height of its parent #wrap div?</p>
    </div>
    <div id="push"></div>
</div>
<div id="footer">
    <div class="container"></div>
</div>

#wrap .container {
    background-color: lightgreen;
}
/* Sticky footer styles  */
html, body {
    height: 100%;
    /* The html and body elements cannot have any padding or margin. */
}
/* Wrapper for page content to push down footer */
#wrap {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    /* Negative indent footer by it's height */
    margin: 0 auto -60px;
}
/* Set the fixed height of the footer here */
#push, #footer {
    height: 60px;
}

#footer {
    background-color: #f5f5f5;
}

1 个答案:

答案 0 :(得分:2)

我有解决问题的方法。我把它从JSFiddle移到了codepen,因为我更喜欢codepen。没有其他原因。 http://cdpn.io/IJHxf

这基本上是我从中得到答案的地方,并且他们以前所未有的方式解释它。 http://v1.reinspire.net/blog/2005/10/11/css_vertical_stretch/

当你实现这个答案的时候,我发现height: auto !important;就是为什么它不能立即起作用的罪魁祸首。在你的#wrap id中注释它,看它是否完全生效。代码笔还有其他更改,可以真正了解正在发生的事情。您真正需要的原始问题是

#wrap .container {
    background-color: lightgreen;
    min-height: 100%;
    height: auto;  /* this line takes care of "more than enough content problem"  */
}


 html, body {
    height: 100%;
    background-color: #dedede;
    padding: 0;
    margin: 0;
}


 #wrap {
    min-height: 100%;
    /* height: auto !important; */
    height: 100%;
    margin: 0 auto -60px;
}

实际上,你可以做什么,并且更有意义的是,而不是评论整行height: auto !important;你可以取消!imporant。例如

#wrap {
    height: auto !important;
    /* changed to */
    height: auto;
}

我改变了一些颜色,让真正发生的事情变得更加明显。你会在codepen中看到它。代码笔中有更多的注释,看看我真的做了什么。

此外,我发现你的粘性页脚因为边距而给我的页面滚动条。对我来说,我用下面的代码摆脱了滚动条。

margin: 0 auto -60px;
/* changed to */
margin: 0 auto -80px;