【问题标题】:Issue with sticky footer at the bottom of the page页面底部的粘滞页脚问题
【发布时间】:2015-01-02 16:21:49
【问题描述】:

我目前正在完善我的code editor's webpage。

我有一个粘性页脚(意味着无论如何都粘在页面底部)。

但是,虽然我不知道怎么做,突然之间我一定是调整了一些东西,它又松开了。

你猜怎么着,我无法修复它。 :S (我不得不承认 CSS 从来都不是我的菜...)

有什么想法吗?


P.S.此问题在所有页面/子页面中都很明显,而不仅仅是主页。


更新:

各位,非常感谢您的超快回复。我认为你做对了。但不是 100% - 也许我没有清楚地解释我需要什么。

添加position:fixed 确实将其固定到底部。但是,假设在主页中,页脚位于内容上方(如顶部导航栏)。这不是我需要的。 “粘性”是指它必须停留在页面底部。如果它是一个短页面,那么它会出现在底部。如果页面很长,只有滚动到页面的最底部才能看到它。

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

将#footer 的位置更改为fixed 似乎工作正常。

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

更新

更新后:移除 body 元素上的 margin-bottom: 45px 并添加 min-height: 100%

body {
    /* margin-bottom: 45px; */
    position: relative;
    min-height: 100% /* for short pages */
}

页脚位置可以保持绝对位置。

更新

html {
    height: 100%;
}

【讨论】:

  • 哦,你太棒了!非常感谢! :-)
  • 所以您的问题是页脚下方的空白间距?
  • 嗯,我想这是一个很好的表达方式。在任何情况下,页脚下方都不得有可见空间。
  • 刚刚尝试(通过 Safari 控制台)您对 osxpeppermint.com/category/themes 页面的更新解决方案。但是,页脚仍然没有移动到底部。看看:i58.tinypic.com/24103z5.png
  • 确实,我忘了说html的高度必须是100%。
【解决方案2】:

我很惊讶没有人能正确回答你的问题,因为你解释得这么清楚。

这就是您仅使用 css 的方式。假设这是您的 html 标记:

<div class="wrapper">
     <p>Your website content here.</p>
</div>

<div class="footer">
     <p>Copyright (c) 2008</p>
</div>

css 应该如下所示:

* {
    margin: 0;
}

html, body {
    height: 100%;
}

.wrapper {
    min-height: 100%;
    /* equal to footer height: */
    margin-bottom: -140px;
 }

.footer {
    height: 140px;
}

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

【讨论】:

  • 非常感谢我的朋友。是的,您的答案是迄今为止最完整的(这基本上就是我想要的)。还有一个问题:请查看此页面(osxpeppermint.com/category/themes)。为什么页面加载时页脚显示在页面下方 10px 处?
  • 在#main css: insead of margin-top: 20px中,尝试使用padding-top: 20px;
  • 嗯...不幸的是我已经尝试过了。而且它似乎并没有像我想要的那样工作......
  • 耶!终于有了我第一个接受的答案。不客气:)
  • 你很棒。我相信你会得到更多被接受的答案! ;-)
【解决方案3】:

改变

#footer {
position: absolute;
...

到

#footer {
position: fixed;
...

【讨论】:

  • 然后使用bottom:-45px;。
【解决方案4】:

试试这个:

#footer {
    background: none repeat scroll 0 0 #ddd;
    border-top: 1px solid #aaa;
    bottom: 0;
    color: #666;
    font-family: "Helvetica Neue",Helvetica,arial,freesans,clean;
    font-size: 16px;
    font-weight: bold;
    height: 45px;
    padding-bottom: 10px;
    padding-top: 10px;
    position: fixed;
    width: 100%;
}

只是将位置改为固定。

【讨论】:

    【解决方案5】:

    您需要将位置固定为底部:0

     #footer {
    position: fixed;
    bottom:0;
    }
    

    【讨论】:

      【解决方案6】:

      为了让某些东西坚持下去,它应该在你的 CSS 中有一个固定的位置。

      您可以在此处找到更多相关信息:http://www.w3schools.com/css/css_positioning.asp

      确保您的页脚及其周围的所有 div 也设置为固定。

      编辑:

      如果您希望您的页脚仅出现在最底部,请将页脚的 margin-bottom 设置为 0px,如下所示:

      #footer {
      
          margin-bottom: 0px;
      
      }
      

      【讨论】:

        【解决方案7】:

        其实有couple of methods在页面底部有一个粘性页脚。看来您使用的是Matthew James Taylor's method,但您的代码中存在一些错误。

        假设给定的标记:

        <body>
            <div class="navbar navbar-inverse navbar-fixed-top"></div>
            <div id="main"></div>
            <div id="footer"></div>
        </body>
        

        您可以按照以下步骤解决问题并实现粘性页脚:

        1. &lt;html&gt; 应该有一个明确的 height 100%。
        2. &lt;body&gt; 应该有:
          1. min-height 的 100%。
          2. 相对定位。即position: relative。
          3. 底部padding作为粘性页脚的高度。
        3. #main 应该有一个顶部 padding 作为固定标题的高度(由您决定)。
        4. #footer 应位于底部 absolutely - position: absolute; bottom: 0;。

        最后,将box-sizing: border-box; 设置为所有元素* {...} 以强制浏览器计算框的大小,包括边框和内边距。

        聚在一起 - Example

        *, *:before, *:after {
          -webkit-box-sizing: border-box;
          -moz-box-sizing: border-box;
          box-sizing: border-box;
        }
        
        html { height: 100%; }
        
        body {
          min-height: 100%;
          margin: 0;
          position: relative;
          padding-bottom: 45px; /* height of the footer */
        }
        
        .navbar {
          height: 30px;
          position: fixed;
          top: 0; left: 0; right: 0;
        }
        
        #main {
          padding-top: 30px; /* height of the fixed positioned header */
        }
        
        #footer {
          position: absolute;
          bottom: 0; left: 0; right: 0;
          height: 45px;
        }
        

        【讨论】:

          【解决方案8】:

          粘性页脚是最简单的方法:DEMO

          footer{
              width:100%;
              height:50px;
              background-color:black;
              position:fixed;
              top:100%;
              left:0;
              margin-top:-50px;
          }
          

          【讨论】:

            【解决方案9】:

            为了更好地理解 CSS,您应该了解“位置”值的工作原理。 html站点中所有元素的默认值为:

            position:static;
            

            现在,对于您自己的问题,position:fixed 是您的解决方案,但不要只使用它而从中学到任何东西。 您可以轻松地试验位置值以查看它们在 html 文档上的工作方式。

            例如,首先使用绝对位置并添加动画 位置固定的类 onscroll 会给你一个很好的滚动效果。

            您可以查看这篇非常详细的文章:https://developer.mozilla.org/en-US/docs/Web/CSS/position。 最后我给你答案:

               #footer {
               position:fixed;
               left:0px;
               bottom:0px;
               height:30px;
               width:100%;
               background:#999;
            }
            
            /* IE 6 */
            * html #footer {
               position:absolute;
               top:expression((0-(footer.offsetHeight)+(document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight)+(ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop))+'px');
            }
            

            /* 更新

            粘页脚效果无法用纯 CSS 实现。问题是您需要一个函数来测量窗口的高度并从顶部为页脚提供正确的边距。
            要做到这一点,你可以使用这个 jQuery 的 sn-p(希望你已经安装了 jQuery):

            $(document).ready(function() {
              var bodyHeight = $("body").height();
              var vwptHeight = $(window).height();
              if (vwptHeight > bodyHeight) {
                $("footer#colophon").css("position","absolute").css("bottom",0);
              }
            });
            

            【讨论】:

            • 请看看我最近的更新。 fixed 并不能完全满足我的需要。
            • 我超过 100% 确信,在 CSS 方面比我知识渊博的人会非常不同意您的观点,因为没有纯 CSS 的解决方案。事实上,我过去也这样做过,而且我也是为这个网站做过的(但是,我一定是做了一些调整,把整个事情搞砸了)。
            【解决方案10】:

            你会用javascript吗? 如果是这样,那么您可以在页面末尾或超时运行此 javascript:


            编辑(我不是强迫使用 javascript,也有很棒的 CSS 答案:)
            var setPosition = function(){
                var foot = document.getElementById('gridContainer2');
                foot.style.position = "relative";
                if((foot.offsetTop + foot.offsetHeight) >= window.innerHeight){
                    foot.style.position = "fixed";
                }
                else{
                    foot.style.position = "relative"; //or whatever you wish
                }
            }
            setTimeout(function(){
                setPosition();
                window.onresize = function(){setPosition();};
            },10);
            

            【讨论】:

            • 如果用户调整窗口大小怎么办?我是否必须将此附加到窗口调整大小事件?听起来太多了,不是吗?
            猜你喜欢
            • 1970-01-01
            • 2012-09-21
            • 2012-02-05
            • 2014-05-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多