在 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
到此这篇关于CSS 实现内容高度不够的时候底部(footer)自动贴底的文章就介绍到这了,更多相关CSS 底部自动贴底内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
1. 给logo添加替代文本 这样有两个好处:屏幕阅读器能识别logo图片代表的含义,...
首先提示,处于安全的需要JavaScript不能直接访问本地资源文件,那怎么办呢?下...
em是何物? em指字体高,任意浏览器的默认字体高都是16px。所以未经调整的浏览器...
创业公司常常能给我们带来惊喜,不拘一格的工作方式,别开生面的商业模式,独树...
大家好,我是民工哥。 一提到监控系统,大家很快就能想到 Zabbix 、 Prometheu s...
一、前言 在了解加密原理前,我们来看看这样一个故事。 小红和小明是情侣,一天...
最近在开发中遇到的需求是:微信扫描课件二维码,播放其对应的课件视频 设计流程...
用什么代码实现?不允许有白色底色产生,因为手机高度不一样 设计图要标准(750...
问题描述 按钮样式为图标+文字,在使用flex布局写垂直居中时,iphone7手机上文字...
复制代码 代码如下: html !--混合框架的布局: 要想实现复杂的页面布局,只需在f...