编程之家收集整理的这篇文章主要介绍了footer固定在页面底部的实现方法总结,编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
HTML代码:
<body> header>头部</main>中间内容footer>底部信息> >
CSS代码:
*{ margin:0; padding:0; } html{ height:100%; } body{ min-height:100%; position:relative; } header{ background: #000; text-align: center;50px; line-height: 50px; color:#fff; } main{ /* main的padding-bottom值要等于或大于footer的height值 */ padding-bottom:100px;#ccc; center; } footer{absolute;#fff; bottom: width:center; background-color: #000; }
实现的效果:
设置height为固定高度值。
优点:footer一直存在于底部。
缺点:中间区域main如果内容不够,不能撑满页面的中间区域。
HTML代码:
div class="container"> div html,body{ .container{ main{ padding-bottom: margin-top:-100px; #000; }
此方法把footer之前的元素放在一个容器里面,形成了container和footer并列的结构:
首先,设置.container的高度至少充满整个屏幕;
其次,设置main(.container最后一个子元素)的padding-bottom值大于等于footer的height值;
最后,设置footer的height值和margin-top负值
。
展示效果跟第一种是一样的,缺点跟第一种也是一样的。
HTML代码:
#000; } 动态为footer添加类fixed-bottom */ .fixed-bottom { fixed; 0;100%; }
JS(jquery)代码:
$(function(){ footerPosition(){ $("footer").removeClass("fixed-bottom"); var contentHeight = document.body.scrollHeight,//网页正文全文高度 winHeight = window.innerHeight;可视窗口高度,不包括浏览器顶部工具栏 if(!(contentHeight > winHeight)){ 当网页正文高度小于可视窗口高度时,为footer添加类fixed-bottom $("footer").addClass("fixed-bottom"); } } footerPosition(); $(window).resize(footerPosition); });
CSS代码:
html,body,#sticker {height: 100%;} body > #sticker { auto; min-height: #stickerCon {padding-bottom: 40px;} #footer {margin-top:-40px; height: 40px; width: 100%; text-align: line-height: color: #ABA498; font-size: 12px; background: #fafafa; border-top:1px solid #E7E7E7;}
HTML代码:
id="sticker"="stickerCon"></="footer">footer>
以上是编程之家为你收集整理的footer固定在页面底部的实现方法总结全部内容,希望文章能够帮你解决footer固定在页面底部的实现方法总结所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入编程之家官方QQ群:1065694478