几种非常不错的方法,收藏学习:原文见https://blog.csdn.net/m0_37070714/article/details/77587753

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

<body>

    <header>header</header>

    <main>content</main>

    <footer>footer</footer>

</body>

  

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;}

  首先,设置body的高度至少充满整个屏幕,并且body作为footer绝对定位的参考节点;

其次,设置main(footer前一个兄弟元素)的padding-bottom值大于等于footer的height值,以保证main的内容能够全部显示出来而不被footer遮盖;

最后,设置footer绝对定位,并设置height为固定高度值。

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

<body>

    <div class="container">

        <header>header</header>

        <main>main content</main>

    </div>

    <footer>footer</footer>

</body>

  

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值 */

  

此方法把footer之前的元素放在一个容器里面,形成了container和footer并列的结构:

首先,设置.container的高度至少充满整个屏幕;

其次,设置main(.container最后一个子元素)的padding-bottom值大于等于footer的height值;

最后,设置footer的height值和margin-top负值。

这种方法没有使用绝对定位,但html结构的语义化不如方法一中的结构清晰。

也可以设置负值的margin-bottom在.container上面,此时html结构变化如下:

<body>

    <div class="container">

        <header>header</header>

        <main>main content</main>

        <div class="empty"></div>

    </div>

    <footer>footer</footer>

</body>

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

.container{min-height:100%;margin-bottom:-100px;}

.empty,footer{height:100px;}

  

使用一个空的div把footer容器推到页面最底部,同时给container设置一个负值的margin-bottom,这个margin-bottom与footer和.empty的高度相等。

虽然多了一个空的div,语义上也不怎么好,但是相比前面为main元素设置padding-bottom的方法有一个明显的好处:当网页内容不满一屏的时候,如果需要为main元素设置边框、背景色的时候,padding-bottom硬生生地撑出了一片空白,真真是有点丑,但是加个空的div之后,布局方式作用在.empty上,对main的css设置就不影响了,算是一个好处吧!

方法三:footer高度任意+js

<body>

    <header>header</header>

    <main>main content</main>

    <footer>footer</footer>

</body>

  

html,body{margin:0;padding: 0;}

header{background-color: #ffe4c4;}

main{background-color: #bdb76b;}

footer{background-color: #ffc0cb;}

/* 动态为footer添加类fixed-bottom */

.fixed-bottom {position: fixed;bottom: 0;width:100%;}

  

$(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);

});

  

footer固定在页面底部的几种方法(转载)的更多相关文章

  1. HTML中footer固定在页面底部的若干种方法

    <div class="header"><div class="main"></div></div> <d ...

  2. 如何将页脚固定在页面底部,4中方法 转载自:W3CPLUS

    原博客地址:http://www.w3cplus.com/css/css-sticky-foot-at-bottom-of-the-page 作为一个Web的前端攻城师,在制作页面效果时肯定有碰到下面 ...

  3. 让footer固定在页面底部(CSS-Sticky-Footer)

    让footer固定在页面底部(CSS-Sticky-Footer)     这是一个让网站footer固定在浏览器(页面内容小于浏览器高度时)/页面底部的技巧.由HTML和CSS实现,没有令人讨厌的h ...

  4. footer固定在页面底部的实现方法总结

    方法一:footer高度固定+绝对定位 HTML代码: <body> <header>头部</header> <main>中间内容</main&g ...

  5. 在不适用fixed的前提下,当内容较少时footer固定在页面底部

    使用css,参考国外的一个解决方法: http://ryanfait.com/resources/footer-stick-to-bottom-of-page/ How to use the CSS ...

  6. Footer固定在页面底部(CSS)

    <style type="text/css"> #wapper{ position: relative; /*重要!保证footer是相对于wapper位置绝对*/ h ...

  7. 利用CSS使footer固定在页面底部

    1.HTML基本结构 <!DOCTYPEhtml> <htmlxmlns="http://www.w3.org/1999/xhtml"> <headr ...

  8. 【转载自W3CPLUS】如何将页脚固定在页面底部

    该文章转载自:W3CPLUS 大漠的文章 http://www.w3cplus.com/css/css-sticky-foot-at-bottom-of-the-page 以下为全文 作为一个Web的 ...

  9. 将HTML页面页脚固定在页面底部(多种方法实现)

    当一个HTML页面中含有较少的内容时,Web页面的footer部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,接下来为大家介绍下如何将页脚固定在页面底部,感兴趣的朋友可以了解下 作为一个 ...

  10. 让footer固定在页面(视口)底部(CSS-Sticky-Footer)

    让footer固定在页面(视口)底部(CSS-Sticky-Footer) 这是一个让网站footer固定在浏览器(页面内容小于浏览器高度时)/页面底部的技巧.由HTML和CSS实现,没有令人讨厌的h ...

随机推荐

  1. 摸鱼日历,新闻简报等一些工作摸鱼日历API接口合集分享

    摸鱼人日历API接口 请求示例(图片输出): https://moyu.qqsuu.cn 请求示例(JSON输出):[推荐] https://moyu.qqsuu.cn/?type=json 调用示例 ...

  2. Java进阶 - [1-1] 六大设计原则

    不要因为某本书而去读,而是因为这本书让你读起来时常有共鸣而去读. 一.单一职责原则 [术语]:SRP,Single Reposibility Principle [定义]:一个类或者模块只负责完成一个 ...

  3. 基于近红外与可见光双目摄像头的人脸识别与活体检测,文末附Demo

    基于近红外与可见光双目摄像头的活体人脸检测原理 人脸活体检测(Face Anti-Spoofing)是人脸识别系统中的重要一环,它负责验证捕捉到的人脸是否为真实活体,以抵御各种伪造攻击,如彩色纸张打印 ...

  4. MAMP使用简单教程

    这个配置,没有域名访问,平时可以放些demo使用,如果需要域名访问请看MAMP PRO教程 启用服务 打开Launchpad中灰色的MAMP,进入界面后,点击Preferences,然后只需拿着鼠标点 ...

  5. 手写Rpc框架-1

    手写Rpc框架 - 导读 git仓库-all-rpc GTIEE:https://gitee.com/quercus-sp204/all-rpc [参考源码 yrpc] 1. Rpc概念 RPC 即远 ...

  6. openssl基础使用(密码学 linux)

    目录        实验原理        实验过程            一.对称加密                1.使用rc4加解密                2.使用AES加解密     ...

  7. ANSYS实体单元施加扭矩方法分析

    ANSYS 结构分析单元与应用-王新敏等(P199) 此处以等截面椭圆柱为例. 对实体单元施加扭矩,主要方法如下: 引入质量单元 MASS21 并新建顶面的中心节点,随后将顶面所有节点通过 cerig ...

  8. 理解Rust引用及其生命周期标识(下)

    在上一篇文章中,我们围绕 "引用必然存在来源" 这一基本概念,介绍了Rust中引用之间的关系,以及生命周期标记的实际意义.我们首先从最简单的单参数方法入手,通过示例说明了返回引用与 ...

  9. Oracle chr() ascii()

    函数简介 实用函数 chr() 和 ascii() chr() 函数将ASCII码转换为字符: ASCII码 –> 字符: ascii() 函数将字符转换为ASCII码: 字符 –> AS ...

  10. Git 覆盖刚刚 commit 的 message

    场景重现 通常噼里啪啦键盘一段猛搓后(写代码啊),然后会 git add . git commit -m "modify semo" # 注意上面 semo 应该是 some,发现 ...