页脚粘在页面底部但与内容重叠

Footer Sticks to bottom of page but overlaps content

我的页脚按照我的意愿粘在页面底部,但是一旦页面上有足够的内容到达页脚,它就会与所述内容重叠。我试过使用 fixed 和 relative 的位置,但都没有像我想要的那样工作。我还尝试了 Whosebug 上提供的其他解决方案,但到目前为止没有任何效果。这是我的代码。

P.S。我对 HTML 和 CSS 不是很有经验,所以如果我遗漏了一些明显的东西,我深表歉意。

    <div>
        <footer>
            <p class="footer">&#169; 2018 The Chipotle Bandits. All rights reserved.</p>
        </footer>
    </div>

.footer {
    color: white;
    text-align: center;
    font-size: 12px;
    position: absolute;
    bottom: 0;
    width: 100%;
    flex: 0 0 50px;
}

JSFIDDLE

您可以在正文中添加 margin-bottom。

添加到您的CSS

body {
  margin-bottom: 30px;
}

发生这种情况是因为页脚是绝对定位的,并且已从元素的正常流中移除。

为了实现这一点,您可以尝试在您的内容中添加填充底部,基本上是在内容底部留一些与页脚高度相等的空白 space。

来自你的fiddle:

</ul>
        </nav>
        <div style="padding-bottom: 2em">
            <p>Pleased him another wa..

你需要设置页脚的height,去掉position: absolute,或者如果你想要一个固定的页脚,你可以使用position: fixed。我也删除了边距。

此外,将 footer 元素样式和您的 .footer class 样式分开。我将 class 重命名为 footer-text。我也使用 flex 将文本居中。

footer {
  /*
  position: fixed;
  bottom: 0;
  */
  background-color: green;
  width: 100%;
  height: 50px;
  flex: 0 0 50px;
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.footer-text {
  color: white;
  text-align: center;
  font-size: 12px;    
}

你需要给正文 padding-bottom 正好是页脚元素的高度,因为页脚绝对放在容器的底部。因此,可以通过在底部留一些间隙来纠正内容重叠。更新页脚正文 css,如下所示

body {
    margin: 0;
    font-family: arial;
    background-color: #363636;
    padding: 0px;
    display: flex;
    flex-direction: column;
    padding-bottom: 38px;
}

另一种情况, 如果您始终希望即使在滚动时也能看到页脚,即粘性页脚,您可以使用固定位置,如下所示

.footer {
    color: white;
    text-align: center;
    font-size: 12px;
    position: fixed;
    bottom: 0;
    width: 100%;
    flex: 0 0 50px;
    background-color:#363636;
    z-index: 10;
    margin: 0;
    padding: 10px 0;
}