一、什么是sticky footer

如果页面内容不够长的时候,页脚块粘贴在视窗底部;如果内容足够长时,页脚块会被内容向下推送,我们看到的效果就如下面两张图这样。这种效果基本是无处不在的,很受欢迎。

二、第一种方式,利用margin和padding实现

先看效果

下面是代码

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>margin,padding实现sticky footer</title>
<style>
html, body, p {
padding: 0;
margin: 0;
height: 100%;
} body {
min-height: 100%;
} #wrapper {
min-height: 100%;
background: yellow;
} #content {
padding-bottom: 50px;
vertical-align: center; }
#text-content{
height: 500px;
}
#footer {
margin-top: -50px;
height: 50px;
background: wheat;
}
</style> </head>
<body>
<div id="wrapper">
<div id="content">
<div id="text-content">填充内容11111111111111</div>
</div>
</div>
<div id="footer">底部内容</div>
</body>
</html>

可以尝试下在text-content中添加内容,可以发现,底部footer是不会受到影响的,坚挺的固定在底部。

这种套路的思路是,给内容区域设置 min-height: 100%,将 footer 推到屏幕下方

然后给 footer 添加 margin-top,其值为 footer 高度的负值,就能让 footer 回到屏幕底部

需要注意的就是内容区域 content 的 padding、footer 的 height 和 margin, 必须保持一致

这种写法的兼容性非常好,实测 IE7 也能正常展示

但是如果页面的主体布局有其他兼容性问题,Sticky Footer 就需要做一些相应的修改

三、第三种方式,使用flex

代码如下,没有添加兼容性前缀

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>flex Sticky footer</title>
<style>
html, body {
height: 100%;
padding: 0;
margin: 0;
} body {
min-height: 100%;
display: flex;
flex-direction: column;
} #header {
line-height: 50px;
background: wheat;
} #content {
flex: 1;
background: yellow;
} #text-content {
height: 600px;
} #footer {
line-height: 50px;
background: wheat;
}
</style> </head>
<body>
<div id="header">顶部</div>
<div id="content">
<div id="text-content">aasdasd</div> </div>
<div id="footer">底部</div>
</body>
</html>

这种方法就是利用flex布局对视窗高度进行分割。

footer的flex设为0,这样footer获得其固有的高度;

content的flex设为1,这样它会充满除去footer的其他部分。

最新文章

  1. MySql数据库乱码解决方法
  2. linux下git安装
  3. Jmeter笔记1:使用Badboy录制脚本,作为JMeter测试的素材
  4. MHA学习笔记
  5. jQuery操作 input type=checkbox的实现代码
  6. 使用air进行移动app开发常见功能和问题(一)
  7. linux文件系统学习
  8. LinearLayout 和 RelativeLayout
  9. To restore the database on a new host-将数据库恢复至一个新的主机上
  10. 详解MongoDB管理命令
  11. 排查Full GC
  12. css3新特性合集
  13. 教你正确打开async/await关键字的使用
  14. Activiti 框架学习
  15. 黑色半透明镂空遮罩指引效果实现jQuery小插件
  16. RS485 VS 20mA 电流环
  17. 解决升级Spark2.0之后,DataFrame map操作报错
  18. Qt5标准文件对话框类
  19. Getting Started with Processing 第四章总结
  20. Confluence 6 删除和归档空间

热门文章

  1. 微信小程序基本知识
  2. 解析一下阿里出品的泰山版 Java 开发手册
  3. Linux 获取网卡名字列表
  4. 基于java的OpenCV安装和配置
  5. css套路学习(一)
  6. shell基础知识DAY2
  7. 蘑菇街CEO陈琪上市致辞:科技是生产力 美丽也是生产力
  8. 从蓝瘦“想哭”到 SELinux 看操作系统安全何在
  9. Qt之QListWidget:项目的多选与单选设置
  10. MySQL UDF Dynamic Library Exploit in *nix