日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

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

發布時間:2023/12/29 综合教程 36 生活家
生活随笔 收集整理的這篇文章主要介紹了 footer固定在页面底部的几种方法(转载) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

幾種非常不錯的方法,收藏學習:原文見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;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;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固定在页面底部的几种方法(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。