css让页脚固定在底部的方法:首先保证页面中的html、body、container满足【height:100%】;然后使用相对定位【bottom:0】将footer固定在页面底部即可。

本教程操作环境:windows10系统、css3版,该方法适用于所有品牌电脑。
原理分析:
(学习视频分享:css视频教程)
页面中的 html , body , container 都必须满足 height:100% ,这样就可以占满整个屏幕(页面), footer 使用相对定位 bottom:0 ,固定在页面底部,页面主体 page 容器必须要设置一个大于等于 footer 高度的 padding-bottom ,目的就为了将 footer 的高度计算在 page 容器中,这样一来footer 就会始终固定在页面底部了。
实现:
HTML
<div id="container">
<div id="header">Header Section</div>
<div id="page" class="clearfix">
<div id="left">Left Sidebar</div>
<div id="content">Main content</div>
<div id="right">Right sidebar</div>
</div>
<div id="footer">Footer Section</div>
</div>
这里唯一需要注意的就是, footer 容器是被 container 容器包含在内的。
CSS
html,body {
margin: 0;
padding:0;
height: 100%;
}
#container {
min-height:100%;
height: auto !important;
height: 100%; /*IE6不识别min-height*/
position: relative;
}
#header {
background: #ff0;
padding: 10px;
}
#page {
width: 960px;
margin: 0 auto;
padding-bottom: 60px;/*等于footer的高度*/
}
#footer {
position: absolute;
bottom: 0;
width: 100%;
height: 60px;/*脚部的高度*/
background: #6cf;
clear:both;
}
/*=======主体内容部分省略=======*/
从css代码中,我们看到,页面主体 page 设置了一个 padding-bottom ,并且与 footer 的高度是一致的。这里不能使用 margin-bottom 来代替 padding-bottom 。
这个方案有一个缺点: footer 必须要固定高度, page 必须要设置一个大于等于这个高度的 padding-bottom 。如果 footer 不是固定高度的,或者需要对footer做自适应,那么这种方案就不太适合了。
相关推荐:CSS教程
以上就是css如何让页脚固定在底部的详细内容,更多请关注php中文网其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
