Bootstrap 4 - 粘性页脚 - 动态页脚高度

4

我需要在我的网页上放置一个固定的底部,但是我没有为我的底部设置明确的高度。在较小的屏幕上,行会重新调整大小,底部变得更长。

因此,getbootstrap提供的默认粘性底部示例不起作用,因为它需要固定的底部高度。

有什么方法可以实现这个效果吗?

/* Sticky footer styles
-------------------------------------------------- */
html {
  position: relative;
  min-height: 100%;
}
body {
  /* Margin bottom by footer height */
  margin-bottom: 60px;
}
.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  /* Set the fixed height of the footer here */
  height: 60px;
  line-height: 60px; /* Vertically center the text there */
  background-color: #f5f5f5;
}


/* Custom page CSS
-------------------------------------------------- */
/* Not required for template or sticky footer method. */

body > .container {
  padding: 60px 15px 0;
}

.footer > .container {
  padding-right: 15px;
  padding-left: 15px;
}

code {
  font-size: 80%;
}

可能是如何制作流体粘性页脚的重复问题。 - Tieson T.
不是重复问题,那个问题已经很老了,而且这个问题特定于Bootstrap 4。 - Adarsha
3个回答

23

现在Bootstrap 4使用flexbox布局,可以通过以下方式实现粘性底部...

<wrapper class="d-flex flex-column">
    <nav>
    </nav>
    <main class="flex-fill">
    </main>
    <footer>
    </footer>
</wrapper>

body, wrapper {
   min-height:100vh;
}

.flex-fill {
   flex:1 1 auto;
}

示例:Bootstrap 4.0粘性页脚

注意:flex-fill实用类在Bootstrap 4.1及更高版本中已经包含。因此,在该版本之后,不再需要额外的CSS来使用flex-fill。

Bootstrap 4.1以来,有一个min-vh-100实用类,这意味着您不需要额外的CSS。

示例:Bootstrap 4.1+粘性页脚


6

一种非常简单的方法是使用导航栏作为页脚。它带有一个非常方便的fixed-bottom类。要更改间距,您可以在此处查看文档... https://getbootstrap.com/docs/4.2/utilities/spacing/

<nav class="navbar fixed-bottom navbar-light bg-light">
  <a class="navbar-brand" href="#">Fixed bottom</a>
</nav>

最好和最简单的我所发现的。 - Daniel Butler

0

这个可以工作,但不是“正确的方法”,因为BS4现在是flexbox。 - user4051844

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接