黑松山资源网 Design By www.paidiu.com
在 UI 切图过程中,页面往往由三个部分组成,头部、内容和底部。当页面的内容高度不够撑满屏幕,底部(footer)就跟着内容浮动上来了,小屏幕由于高度有限看不出来异常,但如果是大屏的话,底部下面变会多出很多空白,非常影响美观。
方案 1:Flex-Box
头部使用 position: sticky; 吸顶,再使用盒子( main )来包住内容( container > content )和底部( footer ),这个盒子设置最小高度为除头部外的剩余屏幕高度: min-height: calc(100vh - 50px); ,盒子里面使用弹性布局( flex: 1 1 auto; )让内容区域自动撑开,而底部保持不变( flex: 0 0 auto; ),这样就有了 内容不够时底部自动吸底,内容足够时底部自动下移 的效果。
示例:
<html> <head> <title>CSS 实现底部(footer)贴底 - 方案 1:Flex-Box</title> <style> body { margin: 0; } header { height: 50px; background: #20c997; position: sticky; top: 0; } main { display: flex; flex-flow: column nowrap; min-height: calc(100vh - 50px); } .container { flex: 1 1 auto; } .content { background: #0d6efd; } footer { flex: 0 0 auto; background: #fd7e14; } </style> </head> <body> <!--头部--> <header> header </header> <main> <div class="container"> <!--内容--> <div class="content"> content </div> </div> <!--底部--> <footer> footer </footer> </main> </body> </html>
在线演示: https://codepen.io/mazeyqian/pen/rNeymdG
优点:底部高度可自由撑开。
缺点:低版本浏览器有兼容性(Flex-Box & Calc)问题。
方案 2:底部负距离 margin
内容区设置最小高度铺满页面,然后底部设置等高的负距离 margin 。
示例:
<html> <head> <title>CSS 实现底部(footer)贴底 - 方案 2:底部负距离 `margin`</title> <style> body { margin: 0; } header { height: 50px; background: #20c997; position: sticky; top: 0; } .container { min-height: calc(100vh - 50px); } .content { background: #0d6efd; } footer { height: 50px; margin-top: -50px; background: #fd7e14; } </style> </head> <body> <!--头部--> <header> header </header> <div class="container"> <!--内容--> <div class="content"> content </div> </div> <!--底部--> <footer> footer </footer> </body> </html>
在线演示: https://codepen.io/mazeyqian/pen/eYZvjzr
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 《GTAOL》PC版时隔多年更新反作弊!小助手宣布跑路
- EA称AI是其业务核心!能提高开发效率、节约成本
- 卫华.1990-太阳升【BMG】【WAV+CUE】
- 呼吸乐队.1992-THEBREATHING【深飞】【WAV+CUE】
- 李玟.2008-1994-2008豪华典藏精选2CD【SONY】【WAV+CUE】
- 《张学友 再现歌神的光辉岁月 梦想成真 2CD》[WAV/分轨][1.2GB]
- 《海来阿木 高音测试王》[WAV+CUE][500MB]
- 《许巍 歌声给你生命的力量 珍藏许巍 3CD》[WAV/分轨][2GB]
- 《直到黎明重制版》配置要求推荐
- 《赛博朋克2077》2.13版本更新内容一览
- 《神话时代:重述版》评测:粉丝们的朝花夕拾
- 战锤40K星际战士2先锋职业介绍|先锋职业技能效果一览
- 战锤40K星际战士2重装职业介绍|重装职业技能效果一览
- 交错战线能源发电站推荐角色一览
- 阿波罗乐神之音《第4集出埃及记》[WAV+CUE]