不少网站设计通常是两个部分,content + footer,content里面装的是网站主体内容,footer里面展现网站的注册信息等等,由于网站内容高度不定的缘由,会出现下面两种状况:
1.内容较少时,这个footer固定在在页面的底部。以下所示:
2.内容较长时,footer跟在内容后面滑动,大体表现以下图红色框起来的部分(对应网址:http://www.sbc-mcc.com/):javascript
这个需求在PC端仍是很常见的,我在本身的应用中也遇到了这个问题,今天总结了一下实现这种布局的几个方法。css
为何第一个就采用js控制的呢,由于实不相瞒,当初我第一次遇到这个问题的时候,直接就使用js去解决的(主要是我知道js确定能实现的,因此也就没有花时间去想别的方法)
主要思路是:在页面加载完成后计算屏幕高度 - content内容真实的高度的值,若是差值大于
footer的高度,就给footer的style加上fixed定位,使它固定在屏幕底部。
demo代码以下:html
<!DOCTYPE html> <html> <head> <title>footer沉底效果</title> <style type="text/css"> 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; } </style> </head> <body> <div id="container"> <div id="content"> content </div> <div id="footer"> footer </div> </div> <script type="text/javascript"> let height = document.getElementById('container').clientHeight - document.getElementById('content').clientHeight; // 这里给footer加上另外的class,使其固定 if (height > 100) document.getElementById('footer').classList.add('footer-fixed'); </script> </body> </html>
本着能使用css解决就绝对不使用js的原则,这个方法虽然最容易想到,可是仍是不推荐使用,并且,这段css代码要获取clientHeight,将会致使页面页面重排和重绘,性能考虑上来讲,也不推荐。java
flex布局中的justify-content: space-between;搭配超级好用的min-height,恰好能够知足在content内容不足的时候,footer的沉底效果
demo代码以下:json
<!DOCTYPE html> <html> <head> <title>footer沉底效果</title> <style type="text/css"> 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; } </style> </head> <body> <div id="container"> <div id="content"> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> </div> <div id="footer"> footer </div> </div> </body> </html>
min-height实在是超级好用的一个css属性了,搭配flex轻松实现沉底效果。布局
这个技巧是在讲margin auto的妙用中学到的,在flex格式化上下文中,margin auto会自动去分配剩余空间。这里面咱们能够在footer上使用margin-top:auto来达到沉底效果。post
<!DOCTYPE html> <html> <head> <title>footer沉底效果</title> <style type="text/css"> 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; // 重点代码 } </style> </head> <body> <div id="container"> <div id="content"> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> content <br> </div> <div id="footer"> footer </div> </div> </body> </html>
总结:以上三种方法都属于没什么反作用的,其实实现这种沉底效果还有别的实现方式,可是对其余布局有影响,这里不赘述,以后有了更好的解决方案,再来更新。
PS:以前margin auto没有深刻了解过,了解以后发现仍是很神奇的,推荐右边文章探秘 flex 上下文中神奇的自动 margin性能