Sticky footer 页面底部

Sticky footer:页面内容不够长的时候,页脚块粘贴在视窗底部;如果内容足够长时,页脚块会被内容向下推送。

sticky-footer

实现的方式主要有以下几种:

1.Flexbox解决方案

核心代码:

<body>
  <div class="main">
    content
  </div>
  <div class="footer">footer</div>
 </body>
body{   
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
.main{
  flex: 1;
}
.footer{
  height:50px;
}

100vh(相对于视口的高度。视口被均分为100单位的vh)这里就不需要设置html{height:100%;}

2.calc()解决方案

核心代码:

<body>
  <div class="main">
    content
  </div>
  <div class="footer">footer</div>
 </body>
.main{
  min-height: calc(100vh - 50px - 1em);  /* 1em为元素之间的间距 */
}
.footer{
  background: #21BAA8;
}

这个方法需要注意减去元素之间的边距。

3.absolute解决方案

<body>
  <div class="main">
    content
  </div>
  <div class="footer">footer</div>
 </body>
body{
  margin:0;
  position: relative;
  min-height: 100vh;
  padding-bottom: 50px;
  box-sizing: border-box;
}
.footer{
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 50px;
}

4.负margin解决方案

这个方法需要增加额外的html元素。

<body>
  <div class="main">
    content
    <div class="push"></div>
  </div>
  <div class="footer">footer</div>
 </body>
.main{
  min-height: 100%;
}
.push{
  height: 50px;
}
.footer{
  margin-top: -50px;
  height: 50px;
}

这里也可以设置.mian{margin-bottom: -50px;},思路一样。

5.Grid解决方案

<body>
  <div class="main">
    content
  </div>
  <div class="footer">footer</div>
 </body>
body{   
  min-height: 100vh;
  display: grid;
  grid-template-rows: 1fr auto;
}
.footer{
  grid-row-start: 2;
  grid-row-end: 3;
}

参考:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,790评论 1 92
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,626评论 1 45
  • 转载请声明 原文链接地址 关注公众号获取更多资讯 第一部分 HTML 第一章 职业规划和前景 职业方向规划定位...
    前端进阶之旅阅读 16,599评论 32 459
  • 总是很羡慕有人讲话非常有意思。是因为自己实在不是那样的人。 还做培训工作的时候,每次问学员最喜欢哪位老师的课程,他...
    墨幽雨阅读 165评论 0 1
  • 江十柒阅读 187评论 0 1