“Sticky Footer”,多种实现

所谓“Sticky Footer”是一种网页效果。即:页面的内容不足以撑开整个屏幕的高度时,footer置于屏幕底部;当内容超过一屏时,footer会随着内容下移,在内容底部。
如图:


Sticky Footer

html 布局如下:

 <div class="box">
  <div class="header"></div>
  <div class="content"></div>
  <div class="footer"></div>
</div>

css实现方案:

flex布局

html,
body {
  /* 关键  */
  height: 100%;
}
.box {
  min-height: 100%; /* 关键 设置最小高度*/
  display: flex;
  flex-direction: column; /* 改变主轴方向 */
}
.header,
.footer  {
  height: 100px;
}
.content {
  flex: 1;
  /* 自适应 */
}

absolute

html,
body {
  /* 关键  */
  height: 100%;
}
.box {
  width: 100%;
  min-height: 100%; /* 关键 */
  position: relative; /* 相对定位 */
}
.header {
  height: 100px;
}
.content {
  padding-bottom: 100px;
}
.footer {
  height: 100px;
  position: absolute; /* 绝对定位 */
  bottom: 0;
  left: 0; /* IE 需要加  */   
}

flex、flex-shrink、margin-top、vh(css单位)

.box {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.header {
  height: 100px;
  flex-shrink: 0;
}
.footer {
  height: 100px;
  /* 自动填满flex布局之后剩余的空间 */
  margin-top: auto;
  /* 子元素的缩小比例 默认为1: 空间不足时默认会缩小 */
  flex-shrink: 0;
}

flex、vh(css单位)

.box{
  /* 关键 */
  min-height: 100vh;  
  display: flex;
  flex-direction: column;
}
.header,
.footer {
  height: 100px;
}
.body{
  flex: 1;
}

min-height、calc

.header,
.footer {
  height: 100px;
}
.content {
  min-height: calc(100vh - 200px);
}

table

html,
body {
  height: 100%;
}
.box {
  display: table;
  width: 100%;
  min-height: 100%;
}
.header,
.footer {
  height: 100px;
}
.content {
  display: table-row;
  height: 100%;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,709评论 1 92
  • H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇阅读 10,162评论 0 26
  • 移动开发基本知识点 一.使用rem作为单位 html { font-size: 100px; } @media(m...
    横冲直撞666阅读 8,957评论 0 6
  • 30年跌跌撞撞、患得患失,想想都是在与自己较劲,和对生活的不认同,一路下来回到原点,越发觉得心要平稳,路要走...
    刘鑫95880阅读 1,432评论 0 0

友情链接更多精彩内容