【问题标题】:CSS- position footer at the bottom of the page [duplicate]CSS-页面底部的位置页脚[重复]
【发布时间】:2017-10-03 19:52:10
【问题描述】:

我正在尝试将页脚放在页面底部,但不粘。当没有足够的内容覆盖屏幕高度时,我会遇到问题,然后页脚不在底部。我试图在 CSS 中这样设置它:

body {
  position: relative;
  margin: 0;
  padding-bottom: 8rem;
  min-height: 100%;
  overflow-x: hidden;
}

footer {
  position: absolute;
  display: flex;
  align-items: center;
  height: 4rem;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: $gray;
}

只有当有足够的内容时,此页脚才会出现在页面底部。但是,当没有足够的内容时,它就在底部之上。这是示例:

Here 是小提琴。

当我添加到 css 文件时,就像答案中建议的那样:

html, body {
  height: 100%;
}

然后,我对有很多内容的页面有问题,然后页脚在页面加载时位于底部,但是当我开始向下滚动时,它会随着内容上升,如图所示:

【问题讨论】:

  • 请张贴html代码。如果你能创造一个小提琴,那将是最好的。

标签: html css


【解决方案1】:

默认情况下,正文根据内部内容采用高度。只需为此添加以下 CSS

html, body {
    height: 100%;
}

html, body {
  height: 100%;
}
body {
  position: relative;
  margin: 0;
  min-height: 100%;
  overflow-x: hidden;
}

footer {
  position: absolute;
  display: flex;
  align-items: center;
  height: 4rem;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: $gray;
  background: red;
}
<footer>Footer Container</footer>

【讨论】:

  • 当没有足够的内容时效果很好,这是问题所在,但是现在在内容很多的页面上,页脚位于底部,但是当我向下滚动时它不在底部了。
  • 你能分享你的代码吗
  • 我在我的问题中添加了小提琴,您可以在其中看到完整的代码
  • 滚动后进入底部,再次检查
  • 不确定,您的意思是什么,但我已经发布了页面上正在发生的事情的图片,其中包含大量包含此代码的内容
【解决方案2】:

这可能与显示属性有关:尝试将其添加到页脚 css 中:

display: block;

这样你的页脚css就变成了:

footer {
  position: absolute;
  display: block;
  align-items: center;
  height: 4rem;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: $gray;
}

如果你不想去掉 display: flex;然后尝试将其包装在任何父 div 中,然后应用 display 属性。

【讨论】:

  • 感谢您的建议,但很遗憾,这没有帮助。
  • 然后尝试位置:固定;尝试使用不同的组合。
【解决方案3】:

在页脚 css 块中将位置值更改为相对值。可能是因为绝对你遇到了这个问题。如果我错了,请纠正我。

【讨论】:

  • 不是这样的。
  • 请试试这个并告诉我。它对我有用。要进行的更改位置:绝对,宽度:100%,底部:0px,左侧:0px
猜你喜欢
  • 2016-06-13
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 2013-01-04
  • 2013-09-13
  • 2016-12-01
  • 2012-01-26
  • 2011-08-01
相关资源
最近更新 更多