CSS能实现的布局有很多,这篇文章给大家分享的是Sticky footer布局的实现。小编觉得挺实用的,因此分享给大家做个参考,文中示例代码介绍的很详细,感兴趣的朋友接下来一起跟随小编看看吧。
什么是sticky footer布局?
我们常见的网页布局方式一般分为header(页头)部分,content(内容区)部分和footer(页脚)部分。当页头区和内容区的内容较少时,页脚区不是随着内容区排布而是始终显示在屏幕的最下方。当内容区的内容较多时,页脚能随着文档流撑开始终显示在页面的最下方。这就是传说中的Sticky footer布局。是不是很容易理解。不理解的小伙伴也没关系下面我就举个简单的例子。
一般指手机页面中,当内容高度撑不满一屏时,页脚紧贴屏幕底部;当内容高度超过一屏时,页脚紧随其后。
方法一:flex弹性盒子布局
查看演示请狠狠地点击:flex弹性盒子布局实现sticky footer
<div class="container"> <div class="content">内容</div> <div class="footer">页脚</div> </div>
body { margin: 0; } .container { display: flex; flex-direction: column; min-height: 100vh; } .content { flex: 1; /*非必须*/ width: 100%; height: 300px; line-height: 300px; text-align: center; color: #fff; font-size: 30px; font-weight: bold; background-color: #71a8da; /*非必须*/ } .footer { height: 60px; /*非必须*/ width: 100%; line-height: 60px; text-align: center; color: #fff; font-size: 30px; font-weight: bold; background-color: #f85f2f; /*非必须*/ }
方法二:padding-bottom+负margin-top
查看演示请狠狠地点击:padding-bottom+负margin-top实现sticky footer
<div class="wrapper"> <div class="content">内容</div> </div> <div class="footer">页脚</div>
body { margin: 0; } .wrapper { width: 100%; min-height: 100vh; } .content { /*padding-bottom应等于footer的高度*/ padding-bottom: 60px; /*非必须*/ width: 100%; height: 400px; line-height: 400px; text-align: center; color: #fff; font-size: 30px; font-weight: bold; background-color: #71a8da; /*非必须*/ } .footer { /*margin-top应等于footer高度的负值*/ margin-top: -60px; height: 60px; /*非必须*/ width: 100%; line-height: 60px; text-align: center; color: #fff; font-size: 30px; font-weight: bold; background-color: #f85f2f; /*非必须*/ }
提示:两个/**非必须**/
之间的部分不是实现sticky footer布局的必要代码,只是一些辅助样式,可以删除
以上就是关于Sticky footer布局的实现介绍了,本文代码仅供参考,需要的朋友可以了解看看,希望对大家学习CSS布局的实现有帮助,想要了解更多CSS布局的实现,可以继续浏览群英网络其他相关的文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理