【问题标题】:Footer with absolute position does not stick on scroll具有绝对位置的页脚不会粘在滚动条上
【发布时间】:2015-03-28 02:23:23
【问题描述】:

我正在尝试制作一个会粘在页面底部的页脚,而不是将其粘在原始窗口的底部位置。当我滚动时,我最终会将页脚粘在页面中间。

我不想修复它并粘在页面上。

当我没有足够的内容滚动时,一切都很好。 (至少看起来是这样)

对应的HTML:

<footer class="footer_div">
  <div class="container">
    <p>Sam Sedighian - No rights reseved!</p>
  </div>
</footer>

对应的CSS:

.footer_div {
  background-image: url("../imgs/dark_dotted2.png");
  color: #818787;
  position: absolute;
  bottom: 0px;
  left: 0px;
  width: 100%;
  height: 40px;
  padding-top: 10px;
  text-align: center;
}

它需要位于页面底部而不是粘性(固定),并且只有在滚动到页面底部时才可见。所以它应该适用于这两个例子:sedighian.github.io/blog/absolute_not_working.htmlsedighian.github.io/blog/absolute_not_working2.html

【问题讨论】:

  • 您的网页无法正常工作,哈哈?
  • 链接似乎已损坏...您可以创建一个小提琴吗?欢迎来到 SO。
  • @Piyush 获得了现在可以使用的链接
  • @ochi 如果这不起作用,我可以创建一个小提琴
  • @SamSedighian 你不能简单地从.footer_div 类中删除position: absolute 吗?那么无论你有多少内容,它都会在内容的末尾流动。这就是你想要达到的目标吗?

标签: css position sticky-footer


【解决方案1】:

这是一个非常微妙的错误。仔细阅读position: absolute documentation

不要为元素留出空间。相反,将其定位在相对于其最近定位的祖先或包含块的指定位置。

footer 没有任何定位的祖先。注意the Bootstrap example,they explicitly declare position: relative on html.

此外,您需要添加一个与页脚高度相等的padding-bottom,以便为其保留空间。

试试:

html {
    min-height: 100%;
    position: relative;
}

body {
    padding-bottom: 40px;   
}

【讨论】:

  • 有趣!然而,引导程序的粘性页脚解决方案使用position: absolute,所以我想知道它是否可能比这更多......?见getbootstrap.com/examples/sticky-footer
  • 其实我并不希望有一个固定的页脚。无论我有多少内容,我都希望页脚留在页面底部
  • 在这种情况下,您只需将 CSS 移动到 footer 而不是内部的 div。如果您发布了一个有效的链接,我可以确认此修复。
  • 这个问题是,当我在博客文章的主体中有内容和没有内容时,它对两者都不起作用。查看我添加到问题中的链接,看看我指的是什么
  • 再次编辑以更紧密地反映 Bootstrap 的 CSS。这在这两种情况下都有效。
猜你喜欢
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 2012-06-16
相关资源
最近更新 更多