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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号