需要注意的关键点是,页脚的高度不会固定,而会随其内容而变化。
当我说“粘性页脚”时,我使用的是通用定义,“即页脚永远不会高于视口底部,但如果有足够的内容,它将被隐藏,直到用户向下滚动足够的距离才能看到它。”
还要注意,我不需要支持旧版浏览器。如果CSS display: table
和相关属性在此处有帮助,则可以使用。
需要注意的关键点是,页脚的高度不会固定,而会随其内容而变化。
当我说“粘性页脚”时,我使用的是通用定义,“即页脚永远不会高于视口底部,但如果有足够的内容,它将被隐藏,直到用户向下滚动足够的距离才能看到它。”
还要注意,我不需要支持旧版浏览器。如果CSS display: table
和相关属性在此处有帮助,则可以使用。
这里所有其他的解决方案都已经过时,要么使用JavaScript,要么使用 table
的技巧。
随着 CSS flex 模型 的出现,解决可变高度的粘性页脚问题变得非常容易:虽然大多数人更熟悉 Flexbox 用于水平方向上的布局,但它同样适用于垂直布局问题。你只需将垂直区域包裹在弹性容器中,并选择要扩展的哪些区域。它们将自动占用容器中所有可用的空间。
请注意标记和 CSS 是多么简单。没有任何 table
技巧等。
96%+ 的今天使用的浏览器 都支持弹性模式。
html, body {
height: 100%;
margin: 0; padding: 0; /* to avoid scrollbars */
}
#wrapper {
display: flex; /* use the flex model */
min-height: 100%;
flex-direction: column; /* learn more: http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ */
}
#header {
background: yellow;
height: 100px; /* can be variable as well */
}
#body {
flex: 1;
border: 1px solid red;
}
#footer{
background: lime;
}
<div id="wrapper">
<div id="header">Title</div>
<div id="body">Body</div>
<div id="footer">
Footer<br/>
of<br/>
variable<br/>
height<br/>
</div>
</div>
display: table-cell
来组织你的页面部分,而不是正常的 display: block
。
HTML<body class="Frame">
<header class="Row"><h1>Catchy header</h1></header>
<section class="Row Expand"><h2>Awesome content</h2></section>
<footer class="Row"><h3>Sticky footer</h3></footer>
</body>
CSS
的翻译是:CSS
.Frame {
display: table;
table-layout: fixed;
height: 100%;
width: 100%;
}
.Row {
display: table-row;
height: 1px;
}
.Row.Expand {
height: auto;
}
table-layout: fixed;
。如果没有这个属性,在IE浏览器中可能会出现宽度问题。无论最大宽度如何,内容都会溢出。 - Philipp Michael你可以通过以下方式将页脚固定在视口底部:
position: fixed;
bottom: 0;
然而,这将使它即使有内容也会出现。
为了防止这种情况,您需要一些JavaScript:
(window.onscroll = function() {
var foot = document.getElementById('footer');
foot.style.position = "static";
if( foot.offsetTop < document.documentElement.scrollTop + document.body.scrollTop + document.documentElement.offsetHeight - foot.offsetHeight)
foot.style.position = "fixed";
})();
((...)();
包装器使得onscroll函数在页面加载时被调用一次,因为这也是必要的)
(上述函数未经测试,但应该可以工作-如果不能,请告诉我,我会制作一个实际的测试页面)
min-height: 100%
而不是height: 100%
。这会导致使用min-height
时整个页面可以滚动,但只有body部分可以使用height
来滚动。 - Milimetricmin-height: 100vh
。 - ospider