美文网首页
footer置于页面最底部的方法

footer置于页面最底部的方法

作者: arlene112 | 来源:发表于2017-07-12 18:59 被阅读252次

首先你需要判断你的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>

相关文章

  • footer置于页面最底部的方法

    首先你需要判断你的footer是固定高度还是任意高度的,因为二者的方法有所却别, 现在先来说情形一,footer高...

  • HTML的footer置于页面最底部的方法

    方法一:footer高度固定+绝对定位 效果: image.png 方法二:footer高度固定+margin负值...

  • Sticky-footer 布局

    在一般的页面中,footer应该是置于内容底部,如果内容高度不足,也应该让footer紧贴底部 demo 使用fl...

  • CSS Footer固定底部处理

    Footer应用场景 自适应内容高度展示在页面最底部 固定于浏览器窗口底部 Footer 自适应内容高度展示在页面...

  • [零碎] footer置于页面底部

    footer置于底部很常用的写法, 一般就直接用absolute或者fixed来定位既可 方法一: 直接使用绝对定...

  • footer的各种写法

    ** footer放置的最好情况:**1.页面内容少,无法撑开,在可视窗口最底部 2.页面内容多,在页面最底部 c...

  • HTML+CSS底部footer两种固定方式

    网页常见的底部栏(footer)目前有两种: 一、永久固定,不管页面的内容有多高,footer一直位于浏览器最底部...

  • HTML5将footer置于页面最底部的方法(CSS+JS)

    JavaScript: CSS:

  • CSS方案总结

    一、Sticky Footer效果 无论页面内容高度如何变化,footer始终在页面底部的固定位置 二、垂直居中 ...

  • flex常用布局

    Sticky Footer 当页面内容高度小于可视区域高度时,footer 吸附在底部;当页面内容高度大于可视区域...

网友评论

      本文标题:footer置于页面最底部的方法

      本文链接:https://www.haomeiwen.com/subject/uqpecxtx.html