sticky footer
来源:互联网 发布:在手机淘宝上如何开店 编辑:程序博客网 时间:2024/05/22 16:31
在项目中前端er会遇到这种需求,页脚固定,这个固定指的是当内容较少时,页脚固定在浏览器底部(这种情况我们会考虑position:fixed),当内容足够多,浏览器视口高度小于内容高度时,页脚固定资页面内容页面的底部(这时会发现position:fixed满足不了需求)。
content内容较少
//html<header> I am header</header><div class="content">内容高度低</div><footer></footer>//cssfooter { position: fixed; width: 100%; height: 60px; background: red; bottom: 0; left: 0;}
demo
content内容足够多,用fixed布局会发现页脚footer会覆盖在内容上
//html <header> I am header</header> <div class="content"> <p>test</p> ...<!--a lot of p here--> <p>test</p> </div> <footer></footer> //css footer { position: fixed; width: 100%; height: 60px; background: red; bottom: 0; left: 0; }
demo
很明显这不是我们想要的,解决上面的问题,我们需要用到”sticky footer”。
sticky footer应用场景是:当内容足够少时,页脚会固定在浏览器底部,当内容足够多时,页脚会固定在页面底部,这里的足够少和足够多也就是内容区的高度相对浏览器或者视口的高度比较而言。
实现sticky footer的方式有很多种,本文介绍一种常用的方法,或者说是通用方法。
内容较少
//html<body><div class="container"> <header> I am header</header> <div class="content"> <p>test</p> <p>test</p> <p>test</p> </div></div><footer>footer</footer></body>//csshtml,body { height: 100%; margin: 0; padding:0; } header { background: green; } .container { min-height: 100%; height: auto; overflow: auto; } .content { padding-bottom: 60px; } footer { position: relative; width: 100%; height: 60px; margin-top: -60px; background: red; clear: both; }
demo
这里的关键是设置
1)html,body的height为100%,margin padding为0,如果html,body的高度没有设置为100%,那么就无法实现这样的效果(可以自己尝试)。
2)header和content用一个容器container div包裹,设置容器的min-height为100%,height:auto。
3)设置content的padding-bottom,主要一定不能写成margin-bottom
这里值得注意的是如果container容器只有content div需要设置overflow:auto,否则即使内容很少,也会出现滚动条,所以无论container是否只有content内容,一般都设置overflow:auto,这样可以避免出现滚动条。
content里面有很多内容,比如有很多p段落。这时footer没有固定在视口的底部,而是固定在页面的底部。
demo
- Sticky Footer
- sticky footer
- css sticky footer
- CSS sticky footer
- sticky footer 布局
- sticky-footer布局
- CSS Sticky Footer 布局
- CSS Sticky Footer
- CSS Sticky Footer
- CSS Sticky Footer布局
- sticky footer布局
- css sticky footer布局
- Sticky footer布局
- css sticky footer
- sticky footer 布局
- sticky footer布局
- sticky footer设计
- css sticky footer布局
- 数据库概论
- 如何成为一名程序员面霸
- springboot+Rabit实战一:(Rabbit MQ windows 环境搭建)
- vue-自定义组件传值
- Java随机生成中文姓名+手机+邮编+地址
- sticky footer
- List的clear()方法和removeAll()方法 的区别
- VMware虚拟机安装
- Linux系统中编译boost 1.65.1库的方法
- 数据结构实验之查找四:二分查找
- 关于sql中逻辑主键和业务主键的理解
- 1478 Problem A总成绩排序(结构体专题)——软四课堂小测试2——2017-12-22
- C 数据结构 笔记(四) —— 队列
- Linux inode、block、文件类型、软硬链接等相关文件的知识