栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Html/CSS > CSS教程

详解网站footer沉底效果的三种解决方案

CSS教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

问题背景

很多网站设计一般是两个部分,content + footer,content里面装的是网站主体内容,footer里面展示网站的注册信息等等,因为网站内容高度不定的原因,会出现下面两种情况:

1.内容较少时,这个footer固定在在页面的底部。如下所示:



2.内容较长时,footer跟在内容后面滑动,大致表现如下图红色框起来的部分:



这个需求在PC端还是很常见的,我在自己的应用中也遇到了这个问题,今天总结了一下实现这种布局的几个方法。

方法1 使用js计算

为什么第一个就采用js控制的呢,因为实不相瞒,当初我第一次遇到这个问题的时候,直接就使用js去解决的(主要是我知道js肯定能实现的,所以也就没有花时间去想别的方法)

主要思路是:在页面加载完成后计算屏幕高度 - content内容真实的高度的值,如果差值大于

footer的高度,就给footer的style加上fixed定位,使它固定在屏幕底部。

demo代码如下:




    footer沉底效果
    
 div {
     margin: 0,
     padding: 0;
     box-sizing: border-box;
     position: relative;
 }
 html, body {
     width: 100%;
     height: 100%;
 }
 #container {
     width: 100%;
     height: 100%;
 }
 #content {
     background: blue;
 }
 #footer {
     width: 100%;
     height: 100px;
     background: red;
 }
 .footer-fixed {
     position: fixed;
     left: 0;
     bottom: 0;
 }
    




     content 

    
 footer
    





本着能使用css解决就绝对不使用js的原则,这个方法虽然最容易想到,但是还是不推荐使用,而且,这段css代码要获取clientHeight,将会导致页面页面重排和重绘,性能考虑上来说,也不推荐。

方法2 采用flex布局 + min-height

flex布局中的justify-content: space-between;搭配超级好用的min-height,刚好可以满足在content内容不足的时候,footer的沉底效果

demo代码如下:




    footer沉底效果
    
 div {
     margin: 0;
     padding: 0;
     box-sizing: border-box;
     position: relative;
 }
 html, body {
     width: 100%;
     height: 100%;
     margin: 0;
     padding: 0;
 }
 #container {
     width: 100%;
     // 重点代码
     // 虽然不知道container的高度,但是可以设置一个最小高度,这样有利于布局
     min-height: 100%;
     display: flex;
     flex-direction: column;
     justify-content: space-between;
 }
 #content {
     background: blue;
 }
 #footer {
     width: 100%;
     height: 100px;
     background: red;
 }
    




     
 content  
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
footer

min-height实在是超级好用的一个css属性了,搭配flex轻松实现沉底效果。

方法3 巧用flex + margin-top

这个技巧是在讲margin auto的妙用中学到的,在flex格式化上下文中,margin auto会自动去分配剩余空间。这里面我们可以在footer上使用margin-top:auto来达到沉底效果。




    footer沉底效果
    
 div {
     margin: 0;
     padding: 0;
     box-sizing: border-box;
     position: relative;
 }
 html, body {
     width: 100%;
     height: 100%;
     margin: 0;
     padding: 0;
 }
 #container {
     width: 100%;
     min-height: 100%;
     display: flex;
     flex-direction: column;
 }
 #content {
     background: blue;
 }
 #footer {
     width: 100%;
     height: 100px;
     background: red;
     margin-top: auto; // 重点代码
 }
    




     
 content  
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
content
footer

总结:以上三种方法都属于没什么副作用的,其实实现这种沉底效果还有别的实现方式,但是对其他布局有影响,这里不赘述,之后有了更好的解决方案,再来更新。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/215091.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号