首頁 > web前端 > css教學 > 主體

在CSS實現Footer置底的幾種方式

php中世界最好的语言
發布: 2018-03-22 10:34:49
原創
2114 人瀏覽過

這次帶給大家在CSS實現Footer置底的幾種方式,在CSS實現Footer置底的注意事項有哪些,下面就是實戰案例,一起來看一下。

頁腳置底(Sticky footer)就是讓網頁的footer部分始終在瀏覽器視窗的底部。

當網頁內容足夠長以至超出瀏覽器可視高度時,頁腳會隨著內容被推到網頁底部;但如果網頁內容不夠長,置底的頁腳就會保持在瀏覽器視窗底部。

方法一:將內容部分的margin-bottom設為負數

<p class="wrapper">
    <!-- content -->
    <p class="push"></p>
</p>
<p class="footer">footer</p>
登入後複製
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}
.wrapper {
  min-height: 100%;  
  margin-bottom: -50px; /* 等于footer的高度 */
}
.footer, .push {
  height: 50px;
}
登入後複製

1、這個方法需要容器裡有額外的佔位元素(p.push)。

2、p.wrapper的margin-bottom需要和p.footer的-height值一樣,注意是負height。

方法二:將頁腳的margin-top設為負數

為內容外增加父元素,並讓內容部分的padding-bottom與頁腳的height相等。

<p class="content">
  <p class="content-inside">
    <!-- content -->
  </p>
</p>
<p class="footer">footer</p>
登入後複製
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}
.content {
  min-height: 100%;
}
.content-inside {
  padding: 20px;
  padding-bottom: 50px;
}
.footer {
  height: 50px;
  margin-top: -50px;
}
登入後複製

方法三:使用calc()設定內容高度

<p class="content">
  <!-- content -->
</p>
<p class="footer">footer</p>
登入後複製
登入後複製
登入後複製
.content {
  min-height: calc(100vh - 70px);
}
.footer {
  height: 50px;
}
登入後複製

這裡假設p.content和p.footer之間有20px的間距,所以70px=50px+20px

#方法四:使用flexbox彈性盒佈局

以上三種方法的footer高度都是固定的,如果footer的內容太多則可能會破壞佈局。

<p class="content">
  <!-- content -->
</p>
<p class="footer">footer</p>
登入後複製
登入後複製
登入後複製
html {
  height: 100%;
}
body {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}
.content {
  flex: 1;
}
登入後複製

方法五:使用Grid網格佈局

#
<p class="content">
  <!-- content -->
</p>
<p class="footer">footer</p>
登入後複製
登入後複製
登入後複製
html {
  height: 100%;
}
body {
  min-height: 100%;
  display: grid;
  grid-template-rows: 1fr auto;
}
.footer {
  grid-row-start: 2;
  grid-row-end: 3;
}
登入後複製

#相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

React與CSS3實作微信拆紅包動畫

css的background-attachment進階使用方法

#

以上是在CSS實現Footer置底的幾種方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
css
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!