更新
我原来的答案是很久以前的,链接坏了;更新它以使其继续有用。
我包括更新的内联解决方案,以及 JSFiddle 上的工作示例。注意:我依赖于 CSS 重置,尽管我没有将这些样式包含在内。参考normalize.css
解决方案 1 - 边距偏移
https://jsfiddle.net/UnsungHero97/ur20fndv/2/
HTML
<div id="wrapper">
<div id="content">
<h1>Hello, World!</h1>
</div>
</div>
<footer id="footer">
<div id="footer-content">Sticky Footer</div>
</footer>
CSS
html, body {
margin: 0px;
padding: 0px;
min-height: 100%;
height: 100%;
}
#wrapper {
background-color: #e3f2fd;
min-height: 100%;
height: auto !important;
margin-bottom: -50px; /* the bottom margin is the negative value of the footer's total height */
}
#wrapper:after {
content: "";
display: block;
height: 50px; /* the footer's total height */
}
#content {
height: 100%;
}
#footer {
height: 50px; /* the footer's total height */
}
#footer-content {
background-color: #f3e5f5;
border: 1px solid #ab47bc;
height: 32px; /* height + top/bottom paddding + top/bottom border must add up to footer height */
padding: 8px;
}
解决方案 2 - 弹性盒
https://jsfiddle.net/UnsungHero97/oqom5e5m/3/
HTML
<div id="content">
<h1>Hello, World!</h1>
</div>
<footer id="footer">Sticky Footer</footer>
CSS
html {
height: 100%;
}
body {
display: flex;
flex-direction: column;
min-height: 100%;
}
#content {
background-color: #e3f2fd;
flex: 1;
padding: 20px;
}
#footer {
background-color: #f3e5f5;
padding: 20px;
}
这里有一些链接,其中包含更详细的解释和不同的方法:
原始答案
这是你的意思吗?
http://ryanfait.com/sticky-footer/
此方法仅使用 15 行 CSS,几乎没有任何 HTML 标记。更好的是,它是完全有效的 CSS,并且适用于所有主流浏览器。 Internet Explorer 5 及更高版本、Firefox、Safari、Opera 等。
此页脚将永久保留在页面底部。这意味着如果内容超过浏览器窗口的高度,您将需要向下滚动才能看到页脚......但如果内容小于浏览器窗口的高度,页脚将粘在底部浏览器窗口,而不是在页面中间浮动。
如果您在实施方面需要帮助,请告诉我。我希望这会有所帮助。