footer置于页面最底部的方法

首先你需要判断你的footer是固定高度还是任意高度的,因为二者的方法有所却别,

现在先来说情形一,footer高度是固定的。有两种方法:

方法一:footer高度固定+绝对定位

css样式

html{height:100%;}

body{min-height:100%;margin:0;padding:0;position:relative;}

.header{background-color: #ffe4c4;}

.main{padding-bottom:100px;background-color: #bdb76b;}/*main的padding-bottom值要等于或大于footer的height*/

.footer{position:absolute;bottom:0;width:100%;height:100px;background-color: #ffc0cb;}        

html代码

<div class="header"></div>

<div class="main"></div>

<div class="footer"></div>

方法二:footer高度固定+margin负值

css样式

html,body{height:100%;margin:0;padding:0;}

.container{min-height:100%;}

.header{background-color: #ffe4c4;}

.main{padding-bottom:100px;background-color: #bdb76b;}/*main的padding-bottom值要等于或大于footer的height值*/.footer{height:100px;margin-top:-100px;background-color: #ffc0cb;}/*margin-top(负值的)高度等于footer的height值*/

html代码

<div class="container">

     <div class="header"></div>

     <div class="main"></div>

</div>

<div class="footer"></div>

如果footer高度任意,上面两种方法就失效了,要用下面的方法

方法三:footer高度任意+js

css样式

/*动态为footer添加类fixed-bottom*/.fixed-bottom {position:fixed;bottom:0;width:100%;}

<script type="text/javascript">

$(function(){

function footerPosition(){

$("footer").removeClass(".fixed-bottom");

    var contentHeight=document.body.scrollHeight,//网页正文高度

          winHeight=window.innerHeight;// 可是窗口高度,不包括浏览器顶部导航栏

if(!(contentHeight>winHeight)){

//当网页正文高度小于可是窗口高度时,为footer添加类fixed-bottom

$("footer").addClass("fixed-bottom");

}else{

$(footer).removeClass("fixed-bottom");

}

}

footerPosition();

$(window).resize(footerPosition);

});

</script>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 不会用代码框,所以看着有些乱套,,,,html部分 <!DOCTYPE html> 迅雷看看 ...
    这就是个帅气的名字阅读 1,562评论 0 0
  • 1. tab列表折叠效果 html: 能源系统事业部 岗位名称: 工作地点 岗位名...
    lilyping阅读 1,897评论 0 1
  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 9,676评论 0 8
  • 当天真无畏的你 在爱情的磨练里 退去了锋芒 是否是成长 你努力挽回却被拒绝的爱情 已经在那一刻就已经被撕碎了 爱你...
    guanyans阅读 307评论 0 0
  • 品尝了四十八年人世界的滋滋味味,酸酸甜甜,苦苦辣辣,唯独没有深刻的体会到真正的快乐是什么。凤凰涅槃,死即是生,开始...
    段舒琴阅读 275评论 0 0