详解CSS五种方式实现Footer置底
前端  /  管理员 发布于 7年前   193
页脚置底(Sticky footer)就是让网页的footer部分始终在浏览器窗口的底部。
当网页内容足够长以至超出浏览器可视高度时,页脚会随着内容被推到网页底部;但如果网页内容不够长,置底的页脚就会保持在浏览器窗口底部。
方法一:将内容部分的margin-bottom设为负数
<div class="wrapper"> <!-- content --> <div class="push"></div></div><div class="footer">footer</div>
html, body { margin: 0; padding: 0; height: 100%;}.wrapper { min-height: 100%; margin-bottom: -50px; /* 等于footer的高度 */}.footer, .push { height: 50px;}
1、这个方法需要容器里有额外的占位元素(div.push)。
2、div.wrapper的margin-bottom需要和div.footer的-height值一样,注意是负height。
方法二:将页脚的margin-top设为负数
给内容外增加父元素,并让内容部分的padding-bottom与页脚的height相等。
<div class="content"> <div class="content-inside"> <!-- content --> </div></div><div class="footer">footer</div>
html, body { margin: 0; padding: 0; height: 100%;}.content { min-height: 100%;}.content-inside { padding: 20px; padding-bottom: 50px;}.footer { height: 50px; margin-top: -50px;}
方法三:使用calc()设置内容高度
<div class="content"> <!-- content --></div><div class="footer">footer</div>
.content { min-height: calc(100vh - 70px);}.footer { height: 50px;}
这里假设div.content和div.footer之间有20px的间距,所以70px=50px+20px
方法四:使用flexbox弹性盒布局
以上三种方法的footer高度都是固定的,如果footer的内容太多则可能会破坏布局。
<div class="content"> <!-- content --></div><div class="footer">footer</div>
html { height: 100%;}body { min-height: 100%; display: flex; flex-direction: column;}.content { flex: 1;}
方法五:使用Grid网格布局
<div class="content"> <!-- content --></div><div class="footer">footer</div>
html { height: 100%;}body { min-height: 100%; display: grid; grid-template-rows: 1fr auto;}.footer { grid-row-start: 2; grid-row-end: 3;}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号