footer固定在页面底部的几种方法(转载)

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

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

1
2
3
4
5
6
7
8
9
<body>
 
    <header>header</header>
 
    <main>content</main>
 
    <footer>footer</footer>
 
</body>

  

1
2
3
4
5
6
7
8
9
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负值

1
2
3
4
5
6
7
8
9
10
11
12
13
<body>
 
    <div class="container">
 
        <header>header</header>
 
        <main>main content</main>
 
    </div>
 
    <footer>footer</footer>
 
</body>

  

1
2
3
4
5
6
7
8
9
10
11
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结构变化如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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

1
2
3
4
5
6
7
8
9
<body>
 
    <header>header</header>
 
    <main>main content</main>
 
    <footer>footer</footer>
 
</body>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
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%;}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$(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);
 
});

  

posted @   JohnYang819  阅读(692)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示