【问题标题】:Keep footer at the bottom of the page (with scrolling if needed)将页脚保留在页面底部(如果需要,可以滚动)
【发布时间】:2015-03-23 14:00:48
【问题描述】:

我正在尝试在页面底部显示页脚。如果页面长于 1 个屏幕,我希望页脚仅在滚动到底部后显示。所以我不能使用'位置:固定',因为它会一直显示。

我正在尝试复制以下示例:http://peterned.home.xs4all.nl/examples/csslayout1.html

但是,当我使用以下内容时,由于某种原因,页脚显示在我的长页面的一半。

position: absolute; bottom:0 

我有短页和长页,我希望它位于它们的底部。

如何让页脚也保持在长页面的底部?

小提琴

我创建了这些 Fiddles 来显示问题并测试代码。 请在您的解决方案中发布一个工作示例。

我的页脚 CSS:

html,body {
    margin:0;
    padding:0;
    height:100%; /* needed for container min-height */
}

.content {
    position:relative; /* needed for footer positioning*/
    margin:0 auto; /* center, not in IE5 */

    height:auto !important; /* real browsers */
    height:100%; /* IE6: treaded as min-height*/

    min-height:100%; /* real browsers */
}

/* --- Footer --- */
.footerbar {                                position: absolute;
                                            width: 100%;
                                            bottom: 0;

                                            color: white;
                                            background-color: #202020;
                                            font-size: 12px; }

a.nav-footer:link,
a.nav-footer:visited {                      color: white !important; }
a.nav-footer:hover, 
a.nav-footer:focus {                        color: black !important;
                                            background-color: #E7E7E7 !important; }

【问题讨论】:

    标签: html css footer


    【解决方案1】:

    我建议使用“粘性页脚”方法。请参阅以下链接:

    http://css-tricks.com/snippets/css/sticky-footer/

    【讨论】:

    • 更新了 Ryan Fait 粘页脚链接codepen.io/griffininsight/pen/OMexrW
    • 这个方法对我来说效果很好,但页面并不总是“可滚动”
    • 即使在 2019 年底也能提供出色的解决方案!没有 flex 和其他不错的功能。谢谢!
    • @tryptofame,举个例子——不,并不总是可滚动的。
    【解决方案2】:

    用溢出替换高度:自动; & 给身体一个位置

    html,body {
        position:relative; <!--Also give it a position -->
        margin:0;
        padding:0;
        overflow:auto; <!-- HERE -->
    }
    

    将页脚定位为相对于正文

    /* --- Footer --- */
    .footerbar { 
        position: relative; <!-- HERE -->
        width: 100%;
        bottom: 0;
        color: white;
        background-color: #202020;
        font-size: 12px; 
    }
    

    在这种情况下,相对定位您的元素,尤其是您的主要元素,例如页脚,这可能总是更好。

    短页编辑

    min-height:400px; <!-- Give this a real number like 400px 
                      or whatever you want...dont leave it to 100% as -->
    }
    

    【讨论】:

    • 给它一个位置是什么意思?使用您的建议时,我没有看到任何变化。请发布完整的代码以使其工作。编辑:我还没有看到页脚。现在它适用于长页面,但是短页面现在有问题。
    • 你不需要使用 min-height 和 height....只需使用 min-height,并给它一个数字我也可以添加....不要担心“ie6”,我会更担心移动视图。
    • 我知道这是一条旧评论,但谢谢!我遇到了页脚没有停留在底部的问题,然后我意识到无论内容有多大,我的主体和容器都没有扩展。所以我使用了溢出:自动;它奏效了。
    【解决方案3】:

    再一次,这里是 flexbox 的一个干净的地方:flex-grow。

    首先我们来看代码:

    div#container {
      /* The power of flexboxes! */
      display: flex;
      display: -webkit-flex;
      flex-direction: column;
    
      min-height: 100vh;
    }
    
    div#container div#content {
      /* Key part: Eat the remaining space! */
      flex-grow: 1;
    }
    
    div#container footer {
      flex-basis: 100px;
    }
    
    
    
    /* Appearance, not important */
    body {
      margin: 0;
      font-family: Fira Code;
    }
    
    @keyframes changeHeight {
      0% {height: 30px}
      10% {height: 30px}
      50% {height: 400px}
      60% {height: 400px}
      100% {height: 30px}
    }
    
    div, footer {
      color: white;
      text-align: center;
    }
    
    div#content section {
      background-color: blue;
      animation: changeHeight 10s infinite linear;
    }
    
    footer {
      background-color: indigo;
    }
    <div id="container">
      <div id="content">
        <!-- All other contents here -->
        <section>Main content</section>
      </div>
      <footer>
        Footer
        <!-- Footer content -->
      </footer>
    </div>

    如果#content 中的内容无法到达页脚,则flex-grow 会扩展元素以适应剩余空间,因为#container 的最小高度为100vh(即视口高度)。显然,如果#content 加上页脚的高度超过视口高度,#container 将是可滚动的。这样,页脚始终保持在最底部。

    sn-p 中的动画属于#content 中的示例部分,它试图向您展示完全相同的东西:它的高度在30px 和400px 之间变化(将其更改为更大的值如果需要)。

    另外,为了提供信息,请参阅the difference between flex-basis and height (or width)。

    提示:在 CSS3 中,如果某些东西不起作用,请查看弹性框和网格。他们经常提供干净的解决方案。

    希望对你有帮助。

    【讨论】:

    • 这应该在顶部。 Flexbox 确实提供了干净的解决方案 +1
    【解决方案4】:

    现在我们有了非常简单的 flex-box。

     body {
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
    

    注意:我们必须在 body 中只包含两个 div。一个用于页脚,另一个用于其他项目

    【讨论】:

      【解决方案5】:

      有一个很好的页脚教程here。

      演示页面是here。

      基本前提是主体页面被拉伸到页面的100%。最小高度也为 100%。

      然后给页脚以下规则:

      .footerbar {
          clear: both;
          position: relative;
          z-index: 10;
          height: 3em;
          margin-top: -3em;
      }
      

      【讨论】:

      • 现在就是这样。我想我已经像他们展示的那样获得了 100% 的最小高度和高度。但是,如果您查看我的长页面,页脚显示不佳。
      • 此链接已失效。
      【解决方案6】:

      一段时间以来,我们一直在努力解决这个问题。包含多个嵌套 div 以及 hack 和补丁的 div 对我们来说变成了一场噩梦。 总是有惊喜需要更多的hack和更多的补丁。 这是我们已经解决的问题:

      css:

      html, body  {
          margin: 0px;
          padding: 0px;
          height: 100%;
          color: #6f643a;
          font-family: Arial;
          font-size: 11pt; 
      }
      
      form {
         height: 100%;
      }    
      

      正文:

      <table style="z-index: 1; margin: 0px; left: 0px; top: 0px; overflow:auto" border="0"  width="100%" height="100%" cellspacing="0" cellpadding="0">
          <tr>
              <td valign="top" align="center" >
               contents goes here
              </td>
          </tr>
          <tr>
              <td valign="top" bgcolor="gray" align="center" style="padding:20px">
                  <font color="#FFFF00">copyright:Puppy</font>
                  footer goes here
              </td>
          </tr>
      </table>
      

      这就是你所需要的。 - 如果您使用的是 asp.net,请不要忽略表单高度。

      【讨论】:

      • 我喜欢页脚背景一直到短页底部的方式。
      【解决方案7】:
      html,body {
          margin:0;
          padding:0;
          height:100%;
      }
      .content {
          padding:10px;
          padding-bottom:80px;   /* Height of the footer element */
      }
      .footerbar {
          width:100%;
          height:80px;
          position:absolute;
          bottom:0;
          left:0;
      }
      

      如果是 IE7

      <!--[if lt IE 7]>
      <style type="text/css">
          .content { height:100%; }
      </style>
      <![endif]-->
      

      【讨论】:

      • 我已将您的建议添加到示例中。恐怕它没有解决长页面上的问题。
      • 这确实修复了长页面。但是,它为短页面带来了新问题。请参阅我为long page 和shortpage 制作的这些小提琴
      【解决方案8】:

      将“position”设置为“fixed”和“bottom: 0”解决了我的问题。现在它是响应式的,页脚在大屏幕(PC、笔记本电脑)和小屏幕(智能手机)上都能正确显示(并且即使滚动) em>)。

      .footerbar {
        display: flex;
        justify-content: center;
        align-items: center;
        text-align: center;
        position: fixed;
        bottom: 0;
        width: 100vw;
        min-height: 3vh;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-23
        • 2013-07-06
        • 2013-07-27
        相关资源
        最近更新 更多