详解CSS五种方式实现Footer置底
建站知识 2021-07-03 08:16www.168986.cn长沙网站建设
页脚置底(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-设为负数
给内容外增加父元素,并让内容部分的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-: -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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
上一篇:利用CSS3实现进度条的两种姿势详解
下一篇:CSS3实现头像旋转效果
长沙网站设计
- 如何进行东阳SEO关键词优化?
- 边坝哪有关键词排名优化:提升你的网站流量与
- 安国百度优化服务:提升您的在线可见性
- 阜康新手做SEO怎么做
- 山西seo网站排名关键词优化:提升您网站曝光率
- 临沂seo网站排名关键词优化:提高你的网站可见
- 广西SEO网站推广怎样付费比较合理
- 双辽SEO网站推广:提升你的网站可见性与流量
- 辽宁企业网站优化购买方式有哪些
- 提升宝清百度SEO排名的实用技巧与策略
- 静宁百度SEO排名:提升您网站曝光率的关键策略
- 彭州百度SEO排名的提升策略和实施指南
- 广南百度关键词SEO:提升网站排名的关键策略
- 辽宁关键词优化怎么做论坛营销
- 吉林百度seo排名如何做到让用户满意
- 内黄百度优化服务:提升在线可见性的关键