何為Sticky footer布局?
我們常見的網頁布局方式一般分為header(頁頭)部分,content(內容區)部分和footer(頁腳)部分。當頁頭區和內容區的內容較少時,頁腳區不是隨著內容區排布而是始終顯示在屏幕的最下方。當內容區的內容較多時,頁腳能隨著文檔流撐開始終顯示在頁面的最下方。這就是傳說中的Sticky footer布局。是不是很容易理解。不理解的小伙伴也沒關系下面我就舉個簡單的例子。
舉個栗子
當內容較少時,正常的文檔流顯示如下圖:

在正常文檔流中,當內容較少時,頁腳部分不會始終固定在屏幕的最下方。這時候就該讓傳說中的sitcky footer布局出現了。
sticky footer布局效果如下圖所示:

不管內容區有多少內容,頁腳始終顯示在屏幕的最下方,當內容區域超過屏幕的高度時。頁腳會始終顯示在頁面的最底部。現在小伙伴們徹底認識sticky footer的真面目了吧,下面讓我們一起看看它是如何實現的。
Sticky footer布局實現
負margin布局方式
html代碼:
<div class="detail"> <div class="wrapper clearfix"> <div class="title"> <h1>這里是頭部</h1> </div> <div class="main"> <p>這里是main content區域...</p> <p>這里是main content區域...</p> <p>這里是main content區域...</p> <p>這里是main content區域...</p> </div> </div> <div class="footer"> <p>© 2017 No rights reserved.</p> <p>Made with ♥ by an anonymous pastafarian.</p> </div> </div>
css代碼:
div,h1,p{margin:0; padding: 0;} .detail{ position:fixed; overflow:auto; width:100%; height:100%; } .wrapper{ min-height:100%; width:100%; } .title h1{ font-size:40px; text-align: center; } .main{ margin-top:64px; padding-bottom:64px; } .main p{ font-size: 25px; text-align: center; } .footer{ margin:-64px auto 0 auto; font-size:32px; } .footer p{ text-align: center; } .clearfix::after { display: block; content: "."; height: 0; clear: both; visibility: hidden; }注:main里的 padding-bottom和footer里的負margin值要保持一致。
flex布局方式
html代碼:
<header> <h1>Site name</h1> </header> <div class="main"> <p>Bacon Ipsum dolor sit amet...</p> <p>Bacon Ipsum dolor sit amet...</p> <p>Bacon Ipsum dolor sit amet...</p> <p>Bacon Ipsum dolor sit amet...</p> </div> <footer> <p>© 2017 No rights reserved.</p> <p>Made with ♥ by an anonymous pastafarian.</p> </footer>
css代碼:
body{display: flex; flex-flow: column; min-height: 100vh; overflow:auto;} h1{font-size: 60px; text-align: center;} p{font-size: 24px; text-align: center;} .main{flex:1;}flex布局結構簡單,代碼精簡。因為flex存在著兼容性,所以在使用這種方式布局時需要注意。
小結
到這里我們的本次探討就結束了,希望對小伙伴們能有幫助。這也是我第一次記錄博客,有不夠完整的地方希望各位大佬多多包涵,給予指導。sticky footer布局也是css中比較經典的布局,對初學者來說應該熟悉掌握這種布局。當然用的多了自然也就會了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答