使页脚正确粘贴到页面底部

Jam*_*son 203 html css

我试图让我的页脚(只有一个带有一行文本的div)位于屏幕的底部,如果内容不是一直到底部,或者是在内容的底部,如果内容需要滚动条.如果内容不需要滚动条,则它可以很好地工作,但是当内容太长时,页脚仍然位于同一位置,位于内容的顶部.

我的基本div结构是:

<div id="container">
    <div id="body"></div>
    <div id="footer"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

我相应的CSS(有点剥离):

body {
    margin: 0;
    padding: 0;
    height: 100%;
}

html {
    margin: 0;
    padding: 0;
    height: 100%;
}

#container {
    width: 674px;
    min-height: 100%;
    height: 100%;
    position: relative;
    margin: 0 auto;
}

#body {
    width: 616px;
    padding: 5px 14px 5px 14px;
    margin: 0 auto;
    padding-bottom: 20px;
}

#footer {
    position: absolute;
    bottom: 0;
    width: 644px;
    height: 20px;
    margin: 0 auto;
}
Run Code Online (Sandbox Code Playgroud)

小智 197

最简单的解决方案是使用min-height<html>标签和位置<footer>position:absolute;

演示:jsfiddle和SO片段:

html {
    position: relative;
    min-height: 100%;
}

body {
    margin: 0 0 100px;
    /* bottom = footer height */
    padding: 25px;
}

footer {
    background-color: orange;
    position: absolute;
    left: 0;
    bottom: 0;
    height: 100px;
    width: 100%;
    overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)
<article>
    <!-- or <div class="container">, etc. -->
    <h1>James Dean CSS Sticky Footer</h1>
    <p>Blah blah blah blah</p>
    <p>More blah blah blah</p>
</article>
<footer>
    <h1>Footer Content</h1>
</footer>
Run Code Online (Sandbox Code Playgroud)

  • 我也很喜欢 - 谢谢.但是,有一个问题:为什么"溢出:隐藏;"? (5认同)
  • 这是一个非常好的解决方案.唯一的缺点是页脚高度的绝对位置...:/.我仍然使用你的解决方案:) (2认同)
  • 这个答案很好,但是一行一行地抄袭别人,甚至不注明出处,真是太不酷了。-1 因为这个。http://mystrd.at/modern-clean-css-sticky-footer/ (2认同)
  • 为页脚设置绝对值很糟糕,因为它涵盖了短页面上的其他静态元素。 (2认同)

Nic*_*not 30

为什么不使用:{ position: fixed; bottom: 0 }

  • 因为页脚会覆盖你的身体内容. (117认同)
  • -1 coz然后你的页脚将在你滚动时跟随你 (18认同)
  • `position:static`是所有内容的默认定位 - 当内容不够高时,设置不会阻止页脚位于视口底部. (7认同)

San*_*eev 16

我使用的简单解决方案适用于IE8 +

在html上给出min-height:100%,这样如果内容少了,那么页面底部会占据完整的视口高度和页脚.当内容增加时,页脚会随着内容向下移动并保持坚持到底.

JS小提琴演奏演示:http://jsfiddle.net/3L3h64qo/2/

CSS

html{
  position:relative; 
  min-height: 100%;
}
/*Normalize html and body elements,this style is just good to have*/
html,body{
  margin:0;
  padding:0;
}
.pageContentWrapper{
  margin-bottom:100px;/* Height of footer*/
} 
.footer{
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height:100px;
    background:#ccc;
}
Run Code Online (Sandbox Code Playgroud)

HTML

   <html>
    <body>
        <div class="pageContentWrapper">
            <!-- All the page content goes here-->
        </div>
        <div class="footer">
        </div>
    </body>
    </html>
Run Code Online (Sandbox Code Playgroud)


小智 5

用这个吧。它会修复它。

#ibox_footer {
    padding-top: 3px; 
    position: absolute;
    height: 20px;
    margin-bottom: 0;
    bottom: 0;
    width: 100%;
}
Run Code Online (Sandbox Code Playgroud)

  • 如果包含元素在屏幕底部之前结束,则不会 (4认同)