• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

css sticky footer经典布局的实现

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

什么是sticky footer布局?

我们常见的网页布局方式一般分为header(页头)部分,content(内容区)部分和footer(页脚)部分。当页头区和内容区的内容较少时,页脚区不是随着内容区排布而是始终显示在屏幕的最下方。当内容区的内容较多时,页脚能随着文档流撑开始终显示在页面的最下方。这就是传说中的Sticky footer布局。是不是很容易理解。不理解的小伙伴也没关系下面我就举个简单的例子。

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

方法一:flex弹性盒子布局

  • 父容器container的display为flex,并规定项目排列顺序是纵向的
  • content元素的flex为1,即有多余空间就增大
  • 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

  • 容器wrapper需要指定min-height为100vh(vh:视窗高度)
  • 内容写在content容器里,指定padding-bottom为footer容器的高度
  • 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布局的必要代码,只是一些辅助样式,可以删除

 到此这篇关于css sticky footer经典布局的实现的文章就介绍到这了,更多相关css sticky footer内容请搜索极客世界以前的文章或继续浏览下面的相关文章,希望大家以后多多支持极客世界!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
解决CSS浏览器兼容性问题的4种方案发布时间:2022-06-21
下一篇:
css实现两列固定与一列自适应的几种方法发布时间:2022-06-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap