css sticky footer经典布局

作者: 大海爱奔跑 | 来源:发表于2020-02-24 11:04 被阅读0次

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

方法一: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布局的必要代码,只是一些辅助样式,可以删除

相关文章

  • sticky-footer布局的方法

    一、什么是sticky-footer布局?sticky-footer布局是一种经典的布局效果,可概况如下:如果页面...

  • css sticky footer经典布局

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

  • CSS经典布局之Sticky footer布局

    何为Sticky footer布局? 我们常见的网页布局方式一般分为header(页头)部分,content(内容...

  • web前端-Sticky Footer布局

    什么是Sticky Footer布局 Sticky Footer布局实现的效果是, 当页面中的内容高度小于屏幕高度...

  • sticky footer布局

    实例 套路 一个展示内容content的容器wrapper 一个展示footer的容器 wrapper设置最小高度...

  • sticky footer 布局

    在网页设计中,sticky footer设计是最古老的和常见的效果之一,大多数人都应该经历过。它可以概括如下:当页...

  • sticky footer 布局

    一、stick footer 布局介绍 文档包括内容区与页脚区。当内容高度未达到视口的高,页脚一直在视口最底部。当...

  • Sticky footer布局

    Sticky Footer布局概括如下:如果页面内容不够长的时候,页脚粘贴在视窗底部;如果内容足够长,页脚会被内容...

  • CSS Sticky Footer

    HTML CSS

  • CSS五种方式实现Footer置底

    https://css-tricks.com/couple-takes-sticky-footer/页脚置底(St...

网友评论

    本文标题:css sticky footer经典布局

    本文链接:https://www.haomeiwen.com/subject/dmemqhtx.html