<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          【CSS】965- 5種實現(xiàn)CSS底部固定的方法

          共 2391字,需瀏覽 5分鐘

           ·

          2021-05-25 15:23

          今天主要介紹一個Footer元素如何粘住底部,使其無論內(nèi)容多或者少,F(xiàn)ooter元素始終緊靠在瀏覽器的底部。
          我們知道,當內(nèi)容足夠多可以撐開底部到達瀏覽器的底部,如果內(nèi)容不夠多,不足以撐開元素到達瀏覽器的底部時,下面要講的布局就是解決如何使元素粘住瀏覽器底部。

          方法一:全局增加一個負值下邊距等于底部高度

          有一個全局的元素包含除了底部之外的所有內(nèi)容。它有一個負值下邊距等于底部的高度。 
          html代碼:
          <body>  <div class="wrapper">
          content
          <div class="push"></div> </div> <footer class="footer"></footer></body>

          css代碼:

          html, body {  height: 100%;  margin: 0;}.wrapper {  min-height: 100%;
          /* Equal to height of footer */ /* But also accounting for potential margin-bottom of last child */ margin-bottom: -50px;}.footer,.push { height: 50px;}

          這個代碼需要一個額外的元素.push等于底部的高度,來防止內(nèi)容覆蓋到底部的元素。這個push元素是智能的,它并沒有占用到底部的利用,而是通過全局加了一個負邊距來填充。

          方法二:底部元素增加負值上邊距

          雖然這個代碼減少了一個.push的元素,但還是需要增加多一層的元素包裹內(nèi)容,并給他一個內(nèi)邊距使其等于底部的高度,防止內(nèi)容覆蓋到底部的內(nèi)容。 

          HTML代碼:

          <body>  <div class="content">    <div class="content-inside">      content    </div>  </div>  <footer class="footer"></footer></body>

          CSS:

          html, body {  height: 100%;  margin: 0;}.content {  min-height: 100%;}.content-inside {  padding: 20px;  padding-bottom: 50px;}.footer {  height: 50px;  margin-top: -50px;}

          方法三:使用calc()計算內(nèi)容的高度

          HTML

          <body>  <div class="content">    content  </div>  <footer class="footer"></footer></body>

          CSS:

          .content {  min-height: calc(100vh - 70px);}.footer {  height: 50px;}

          給70px而不是50px是為了為了跟底部隔開20px,防止緊靠在一起。

          方法四:使用flexbox

          HTML:

          <body>  <div class="content">    content  </div>  <footer class="footer"></footer></body>

          CSS:

          html {  height: 100%;}body {  min-height: 100%;  display: flex;  flex-direction: column;}.content {  flex: 1;}

          方法五:使用grid布局

          HTML:

          <body>  <div class="content">    content  </div>  <footer class="footer"></footer></body>

          CSS:

          html {  height: 100%;}body {  min-height: 100%;  display: grid;  grid-template-rows: 1fr auto;}.footer {  grid-row-start: 2;  grid-row-end: 3;}

          grid早于flexbox出現(xiàn),但并沒有flexbox被廣泛支持,你可能在chrome  Canary或者Firefox開發(fā)版上才可以看見效果。

          1. JavaScript 重溫系列(22篇全)
          2. ECMAScript 重溫系列(10篇全)
          3. JavaScript設(shè)計模式 重溫系列(9篇全)
          4. 正則 / 框架 / 算法等 重溫系列(16篇全)
          5. Webpack4 入門(上)|| Webpack4 入門(下)
          6. MobX 入門(上) ||  MobX 入門(下)
          7. 120+篇原創(chuàng)系列匯總

          回復“加群”與大佬們一起交流學習~

          點擊“閱讀原文”查看 120+ 篇原創(chuàng)文章

          瀏覽 62
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  看操逼逼| 99综合视频一区 | 国产大鸡吧自拍 | 精品国产精品 | 日本一区二区三区四区五区六区 |