css sticky footer经典布局

作者 : 开心源码 本文共1455个字,预计阅读时间需要4分钟 发布时间: 2022-05-13 共254人阅读

什么是sticky footer布局?
一般指手机页面中,当内容高度撑不满一屏时,页脚紧贴屏幕底部;当内容高度超过一屏时,页脚紧随其后。

image

方法一:flex弹性盒子布局

  1. 父容器container的display为flex,并规定项目排列顺序是纵向的
  2. content元素的flex为1,即有多余空间就增大
  3. footer定义一个高度

查看演示请狠狠地点击:flex弹性盒子布局实现sticky footer

<div class="container">  <div class="content">内容</div>  <div class="footer">页脚</div></div>
body {  margin: 0;}.container {  display: flex;  flex-direction: column;  min-height: 100vh;}.content {  flex: 1;  /*非必需*/  width: 100%;  height: 300px;  line-height: 300px;  text-align: center;  color: #fff;  font-size: 30px;  font-weight: bold;  background-color: #71a8da;  /*非必需*/}.footer {  height: 60px;  /*非必需*/  width: 100%;  line-height: 60px;  text-align: center;  color: #fff;  font-size: 30px;  font-weight: bold;  background-color: #f85f2f;  /*非必需*/}

方法二:padding-bottom+负margin-top

  1. 容器wrapper需要指定min-height为100vh(vh:视窗高度)
  2. 内容写在content容器里,指定padding-bottom为footer容器的高度
  3. footer指定高度和margin-top,并且margin-top为高度的负值

查看演示请狠狠地点击:padding-bottom+负margin-top实现sticky footer

<div class="wrapper">  <div class="content">内容</div></div><div class="footer">页脚</div>
body {  margin: 0;}.wrapper {  width: 100%;  min-height: 100vh;}.content {  /*padding-bottom应等于footer的高度*/  padding-bottom: 60px;  /*非必需*/  width: 100%;  height: 400px;  line-height: 400px;  text-align: center;  color: #fff;  font-size: 30px;  font-weight: bold;  background-color: #71a8da;  /*非必需*/}.footer {  /*margin-top应等于footer高度的负值*/  margin-top: -60px;  height: 60px;  /*非必需*/  width: 100%;  line-height: 60px;  text-align: center;  color: #fff;  font-size: 30px;  font-weight: bold;  background-color: #f85f2f;  /*非必需*/}

提醒:两个/**非必需**/之间的部分不是实现sticky footer布局的必要代码,只是少量辅助样式,可以删除

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » css sticky footer经典布局

发表回复