【问题标题】:Footer Sticks to bottom of page but overlaps content页脚粘在页面底部但与内容重叠
【发布时间】:2018-09-10 10:52:28
【问题描述】:

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

附:我对 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

【问题讨论】:

标签: html css footer


【解决方案1】:

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

添加到您的 CSS

body {
  margin-bottom: 30px;
}

【讨论】:

    【解决方案2】:

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

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

    从你的小提琴:

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

    【讨论】:

      【解决方案3】:

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

      另外,将footer 元素样式和您的.footer 类样式分开。我将课程重命名为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;    
      }
      

      【讨论】:

        【解决方案4】:

        您需要为 body 提供与页脚元素的高度完全相同的 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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-01-21
          • 1970-01-01
          • 2011-07-23
          • 2020-05-15
          • 2016-12-07
          • 2020-03-31
          • 1970-01-01
          • 2016-05-01
          相关资源
          最近更新 更多