常見的sticky footer佈局方式

什麼是sticky footer佈局

我們常見的網站頁面都會把一個頁面分爲:頭部區、內容區、頁腳區,當頭部區和內容區內容較少時,頁腳區能固定在網頁底部,而不是隨着文檔流排布。當頁面內容較多時,頁腳能隨着文檔內容自動撐開,顯示在頁面的最底部。這就是sticky footer佈局。

實現方式

flex 實現

html代碼

<header class="header"></header>
    <main class="content">
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
        <p>近段時間房管局四大金剛</p>
    </main>
    <footer class="footer"></footer>

css代碼

*{
    margin: 0;
    padding: 0;
}
html,body{
    display: flex;
    flex-direction: column;
    min-height: 100%;
    width: 100%;
}
.header{
    background: gray;
    height: 20px;
}
.content{
    flex: 1;
    overflow: auto;
    background: greenyellow;
}
.footer{
    background: pink;
    height: 20px;
}

flex佈局方法簡單代碼少,因爲pc端兼容性不是很好,可以廣泛用於移動端。

負margin佈局方式

html代碼

<div class="wrapper clearfix">
        <div class="title">
            <h1>這裏是頭部</h1>
        </div>
        <div class="main">
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
            <p>近段時間房管局四大金剛</p>
        </div>
    </div>
    <div class="footer">
        <p>© 2017 No rights reserved.</p>
        <p>Made with ♥ by an anonymous pastafarian.</p>
    </div>

css代碼

* {
    margin: 0;
    padding: 0;
    text-align: center;
}

.wrapper {
    min-height: 100%;
    width: 100%;
}

.main {
    margin-top: 64px;
    padding-bottom: 64px;
}

.footer {
    margin: -64px auto 0 auto;
    background: orange;
}

.clearfix::after {
    display: block;
    content: ".";
    height: 0;
    clear: both;
    visibility: hidden;
}

這是兼容性最好的方案,各大瀏覽器都可兼容,就是需要提前知道footer的高度。且結構相對複雜。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章