温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

css让页脚固定在底部的方法

发布时间:2020-11-23 15:35:35 来源:亿速云 阅读:500 作者:小新 栏目:web开发

这篇文章给大家分享的是有关css让页脚固定在底部的方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

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

原理分析:

页面中的 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让页脚固定在底部的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI