css - 位置 :absolute for footer not working as expecting

标签 css footer

<分区>

我已用谷歌搜索但找不到任何可行的解决方案;作为下面的示例,position:absolute 没有在页面底部设置页脚

.footer {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: red;
  color: white;
  text-align: center;
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <div class="footer">
    <p>Footer</p>
  </div>

</body>

</html>

如果我滚动它,我会得到:

enter image description here

我想将页脚固定在页面底部,我可以为谁做这件事?

最佳答案

使用 position: fixed 而不是 position: absolute,请根据您的代码检查下面的工作示例。

.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  background-color: red;
  color: white;
  text-align: center;
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <h2>Fixed/Sticky Footer Example</h2>
  <p>The footer is placed at the bottom of the page.</p>

  <div class="footer">
    <p>Footer</p>
  </div>

</body>

</html>

关于css - 位置 :absolute for footer not working as expecting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51384941/

上一篇:c# - tinyMCE 下拉菜单与 'add hyperlink' 模态断开连接

下一篇:javascript - 使用 Javascript 变量设置样式值不起作用

相关文章:

css - 解决神奇的页脚问题

页脚跨浏览器(IE、FF、Chrome)的 css chrome 问题

CSS - 使图像适合屏幕,这样就不会出现滚动条

c# - 我的 CSS Body 样式不适用于登录页面

html - Bootstrap 网格列无法正常工作

css - 将 CSS 打印媒体与 Xporability/css-to-pdf 结合使用

javascript - 使用JS和SCSS的导航动画,你能告诉我哪里错了吗?

html - 如何在不增加正文大小的情况下使页脚超出页面末尾

css - 高度 100% 不适用于固定页脚 div

html - 缩小浏览器时,响应式页脚显示在部分内容上