zoukankan      html  css  js  c++  java
  • CSS粘住固定底部的5种方法

    本文主要介绍一个Footer元素如何粘住底部,使其无论内容多或者少,Footer元素始终紧靠在浏览器的底部。我们知道,当内容足够多可以撑开底部到达浏览器的底部,如果内容不够多,不足以撑开元素到达浏览器的底部时,下面要讲的布局就是解决如何使元素粘住浏览器底部。需求看下图:·

    原文来自http://caibaojian.com/css-5-ways-sticky-footer.html

    方法一:全局增加一个负值下边距等于底部高度

    有一个全局的元素包含除了底部之外的所有内容。它有一个负值下边距等于底部的高度。

    1 <body>
    2   <div class="wrapper">
    3 
    4       content
    5 
    6     <div class="push"></div>
    7   </div>
    8   <footer class="footer"></footer>
    9 </body>

    CSS代码:

     1 html, body {
     2   height: 100%;
     3   margin: 0;
     4 }
     5 .wrapper {
     6   min-height: 100%;
     7 
     8   /* Equal to height of footer */
     9   /* But also accounting for potential margin-bottom of last child */
    10   margin-bottom: -50px;
    11 }
    12 .footer,
    13 .push {
    14   height: 50px;
    15 }

    演示:

    这个代码需要一个额外的元素.push等于底部的高度,来防止内容覆盖到底部的元素。这个push元素是智能的,它并没有占用到底部的利用,而是通过全局加了一个负边距来填充。

    方法二:底部元素增加负值上边距

    虽然这个代码减少了一个.push的元素,但还是需要增加多一层的元素包裹内容,并给他一个内边距使其等于底部的高度,防止内容覆盖到底部的内容。

    HTML代码:

    1 <body>
    2   <div class="content">
    3     <div class="content-inside">
    4       content
    5     </div>
    6   </div>
    7   <footer class="footer"></footer>
    8 </body>

    CSS:

     1 html, body {
     2   height: 100%;
     3   margin: 0;
     4 }
     5 .content {
     6   min-height: 100%;
     7 }
     8 .content-inside {
     9   padding: 20px;
    10   padding-bottom: 50px;
    11 }
    12 .footer {
    13   height: 50px;
    14   margin-top: -50px;
    15 }

    演示:

    方法三:使用calc()计算内容的高度

    HTML :

    1 <body>
    2   <div class="content">
    3     content
    4   </div>
    5   <footer class="footer"></footer>
    6 </body>

    CSS:

    1 .content {
    2   min-height: calc(100vh - 70px);
    3 }
    4 .footer {
    5   height: 50px;
    6 }

    演示:

    给70px而不是50px是为了为了跟底部隔开20px,防止紧靠在一起。

    方法四:使用flexbox

    关于flexbox的教程,还请查看之前的一篇详细的教程

    HTML:

    1 <body>
    2   <div class="content">
    3     content
    4   </div>
    5   <footer class="footer"></footer>
    6 </body>

    CSS:

     1 html {
     2   height: 100%;
     3 }
     4 body {
     5   min-height: 100%;
     6   display: flex;
     7   flex-direction: column;
     8 }
     9 .content {
    10   flex: 1;
    11 }

    演示:

    方法五:使用grid布局

    HTML:

    1 <body>
    2   <div class="content">
    3     content
    4   </div>
    5   <footer class="footer"></footer>
    6 </body>

    CSS:

     1 html {
     2   height: 100%;
     3 }
     4 body {
     5   min-height: 100%;
     6   display: grid;
     7   grid-template-rows: 1fr auto;
     8 }
     9 .footer {
    10   grid-row-start: 2;
    11   grid-row-end: 3;
    12 }

    演示:

    grid早于flexbox出现,但并没有flexbox被广泛支持,你可能在chrome  Canary或者Firefox开发版上才可以看见效果

  • 相关阅读:
    ◆ C++中通过溢出覆盖虚函数指针列表执行代码
    关于在OnTimer中连续弹出对话框的讨论
    SetTimer
    Windows内核对象
    日志收缩
    暴力求值
    低级问题
    函数限制
    字符串找字段和表
    android错误提示说明汇总
  • 原文地址:https://www.cnblogs.com/qcxc/p/6868438.html
Copyright © 2011-2022 走看看