切图妞

vuePress-theme-reco 切图妞    2020 - 2021
切图妞 切图妞
前端知识梳理
  • Vue
  • 浏览器 & 网络
  • HTML & CSS
  • Web安全
  • 算法
文章分类
  • 前端小麻烦
  • 配置乐园
  • 实战不完全手册
  • 手撕源码
宝藏女孩
  • 模板仓
  • 项目简介
  • GitHub
  • Segmentfault
  • CSDN
时间轴
author-avatar

切图妞

19

Article

18

Tag

前端知识梳理
  • Vue
  • 浏览器 & 网络
  • HTML & CSS
  • Web安全
  • 算法
文章分类
  • 前端小麻烦
  • 配置乐园
  • 实战不完全手册
  • 手撕源码
宝藏女孩
  • 模板仓
  • 项目简介
  • GitHub
  • Segmentfault
  • CSDN
时间轴

前端必懂之Sticky Footer(粘性页脚)

vuePress-theme-reco 切图妞    2020 - 2021

前端必懂之Sticky Footer(粘性页脚)

切图妞 2019-08-12 CSS

Sticky Footer是css的一种布局场景。页脚footer永远固定在页面的底部,页面内容不够长的时候页脚黏在视窗底部,内容足够长时会被向下移动。老式门户网站由于内容过短常常版权页脚前移,移动端特定布局也需要Sticky Footer来解决这些问题。

![图片描述](/img/article/1. gif)


写在最前:Sticky Footer是css的一种布局场景。页脚footer永远固定在页面的底部,页面内容不够长的时候页脚黏在视窗底部,内容足够长时会被向下移动。老式门户网站由于内容过短常常版权页脚前移,移动端特定布局也需要Sticky Footer来解决这些问题。

# 一、利用绝对定位和padding完美兼容

已知底部高度,利用绝对定位和padding完美兼容 https://codepen.io/qietuniu/pen/KYxMwv

  1. 去除标签多余的margin, padding, 给html和body设置100%
  2. 外部容器min-height为100%,使得内容少时也能撑开
  3. 主体内容设置padding-bottom,这个为底部的高度,可以使内容完全显示否则会使主体内容有底部面积大小区域被遮挡
  4. footer高度固定,进行绝对定位

DOM节点

<div class="container">
  <div class="header"></div>
  <div class="section">
  </div>
  <div class="footer">Copyright© 1994-2019 切图妞</div>
</div>
1
2
3
4
5
6

样式代码

html,
body {
  height: 100%;
}
.container {
  position: relative;
  min-height: 100%;
  height: auto !important;
  height: 100%; /*IE6不识别min-height*/
}
.section {
  padding-bottom: 60px;
}
.footer {
  position: absolute;
  width: 100%;
  height: 60px;
  bottom: 0px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 二、利用padding-bottom和margin-top完美兼容

已知底部高度,利用padding-bottom和margin-top完美兼容 https://codepen.io/qietuniu/pen/dLqpdR

  1. 去除标签多余的margin, padding, 给html和body设置100%;
  2. 外部容器min-height为100%,使得内容少时也能撑开
  3. 主体内容设置padding-bottom,这个为底部的高度
  4. footer高度固定,margin-top的值为高度负值,footer与container同级。

DOM节点

<div class="container">
  <div class="header"></div>
  <div class="section">
  </div>
</div>
<div class="footer">Copyright© 1994-2019 切图妞</div>
1
2
3
4
5
6

样式代码

html,
body {
  height: 100%;
}
.container {
  min-height: 100%;
  height: auto !important;
  height: 100%; /*IE6不识别min-height*/
}
.section {
  padding-bottom: 60px;
}
.footer {
  position: relative;
  margin-top: -60px;
  width: 100%;
  height: 60px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 三、新增块级元素填补页脚遮挡

已知底部高度,新增块级元素填补页脚遮挡,实现完美兼容 https://codepen.io/qietuniu/pen/dLqpez

  1. 去除标签多余的margin, padding, 给html和body设置100%;
  2. 外部容器min-height为100%,使得内容少时也能撑开
  3. 主体内容设置margin-bottom,值为底部的高度的负值
  4. footer位置在与container同级,section同级新增块元素. 底部和新增块元素高度一致

DOM节点

<div class="container">
  <div class="header"></div>
  <div class="section">
  </div>
  <div class="placeholder"></div>
</div>
<div class="footer">Copyright© 1994-2019 切图妞</div>
1
2
3
4
5
6
7

样式代码

html,
body {
  height: 100%;
}
.container {
  min-height: 100%;
  height: auto !important;
  height: 100%; /*IE6不识别min-height*/
  margin-bottom: -60px;
}
.placeholder,
.footer {
  height: 60px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 四、用表格属性实现完美兼容

底部不定高度,利用表格属性实现完美兼容 https://codepen.io/qietuniu/pen/QPVKVR

  1. 去除标签多余的margin, padding, 给html和body设置100%
  2. 外部容器min-height为100%; 使得内容少时也能撑开,display属性设置为table
  3. 主体内容display属性设置为table-row,高度设置为100%

DOM节点

<div class="container">
  <div class="header"></div>
  <div class="section">
  </div>
  <div class="footer">Copyright© 1994-2019 切图妞</div>
</div>
1
2
3
4
5
6

样式代码

html,
body {
  height: 100%;
}
.container {
  display: table;
  width: 100%;
  min-height: 100%;
}
.section {
  display: table-row;
  height: 100%;
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 五、calc计算

vh存在兼容有限,一般在移动端使用。100vh可代替body和html的100%来拿到视口高度实现效果 https://codepen.io/qietuniu/pen/NmLRmV

  1. 外部容器使用calc计算,100vh减去底部高度
  2. footer位置与container同级,高度固定
  3. 主体内容display属性设置为table-row,高度设置为100%

DOM节点

<div class="container">
  <div class="header"></div>
  <div class="section">
  </div>
</div>
 <div class="footer">Copyright© 1994-2019 切图妞</div>
1
2
3
4
5
6

样式代码

.container {
  min-height: calc(100vh - 60px);
}
.footer {
  height: 60px;
}
1
2
3
4
5
6

# 六、flex弹性布局

底部不定高度,利用flex弹性布局实现效果,兼容性有限建议移动端使用 https://codepen.io/qietuniu/pen/EJeNYW

  1. 外部容器display设为flex弹性布局,flex-flow设置方向为column纵向并设置最小高度为100vh
  2. 主体内容flex属性设为1

DOM节点

<div class="container">
  <div class="header"></div>
  <div class="section">
  </div>
  <div class="footer">Copyright© 1994-2019 切图妞</div>
</div>
1
2
3
4
5
6

样式代码

.container {
  display: flex;
  flex-flow: column;
  min-height: 100vh;
}
.section {
  flex: 1
}
1
2
3
4
5
6
7
8

# 七、Grid网格布局

底部不定高度,利用Grid网格实现效果,兼容性有限建议移动端使用 https://codepen.io/qietuniu/pen/EJeNYW

  1. 外部容器display设为grid网格布局,grid-template-rows设置一个网格的行,fr单位可以帮助我们创建一个弹列的网格轨道, 它代表了网格容器中可用的空间(就像Flexbox中无单位的值)
  2. header头部的位置放在主体内容内部
  3. footer中grid-row-start和grid-row-end属性设置单元格开始和结束的行线

DOM节点

<div class="container">
  <div class="header"></div>
  <div class="section">
  </div>
  <div class="footer">Copyright© 1994-2019 切图妞</div>
</div>
1
2
3
4
5
6

样式代码

.container {
  min-height: 100vh;
  display: grid;
  grid-template-rows: 1fr auto;
}
.footer {
  grid-row-start: 2;
  grid-row-end: 3;
}
1
2
3
4
5
6
7
8
9

# 结语

以上方法各有优劣,根据使用场景选择合适的方案

场景 方案
兼容性要求高 ①②③
底部不固定高度 ④⑥⑤⑦
PC端建议 ①②
移动端建议 ①②⑥

完整代码

尊重原创,如需转载请注明出处!