CSS經典佈局之Sticky footer佈局

何爲Sticky footer佈局?

咱們常見的網頁佈局方式通常分爲header(頁頭)部分,content(內容區)部分和footer(頁腳)部分。當頁頭區和內容區的內容較少時,頁腳區不是隨着內容區排布而是始終顯示在屏幕的最下方。當內容區的內容較多時,頁腳能隨着文檔流撐開始終顯示在頁面的最下方。這就是傳說中的Sticky footer佈局。是否是很容易理解。不理解的小夥伴也不要緊下面我就舉個簡單的例子。css

舉個栗子

  • 當內容較少時,正常的文檔流顯示以下圖: html

    圖(1)
    在正常文檔流中,當內容較少時,頁腳部分不會始終固定在屏幕的最下方。這時候就該讓傳說中的sitcky footer佈局出現了。

  • sticky footer佈局效果以下圖所示: bash

    圖(2)
    無論內容區有多少內容,頁腳始終顯示在屏幕的最下方,當內容區域超過屏幕的高度時。頁腳會始終顯示在頁面的最底部。如今小夥伴們完全認識sticky footer的真面目了吧,下面讓咱們一塊兒看看它是如何實現的。

Sticky footer佈局實現

  • 負margin佈局方式

html代碼:app

<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

  • 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代碼:spa

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存在着兼容性,因此在使用這種方式佈局時須要注意。code

小結

到這裏咱們的本次探討就結束了,但願對小夥伴們能有幫助。這也是我第一次記錄博客,有不夠完整的地方但願各位大佬多多包涵,給予指導。sticky footer佈局也是css中比較經典的佈局,對初學者來講應該熟悉掌握這種佈局。固然用的多了天然也就會了。cdn

相關文章
相關標籤/搜索