【问题标题】:Bootstrap 4 - Sticky footer - Dynamic footer heightBootstrap 4 - 粘性页脚 - 动态页脚高度
【发布时间】:2018-02-05 07:51:57
【问题描述】:

我需要在我的页面上放置一个粘性页脚,但是我没有为我的页脚设置一个明确的高度。在较小的屏幕上 - 行调整大小和页脚变得更长。

因此,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%;
}

【问题讨论】:

标签: html css twitter-bootstrap bootstrap-4 sticky-footer


【解决方案1】:

既然 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 Sticky Footer

注意:flex-fill 实用程序类是included in the Bootstrap 4.1 and later 版本。因此,在发布之后,将不再需要用于 flex-fill 的额外 CSS。

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

演示:Bootstrap 4.1+ Sticky Footer

【讨论】:

    【解决方案2】:

    一个非常简单的方法是使用导航栏作为页脚。它带有一个非常方便的固定底部类。要更改间距,您可以在此处查看文档...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>
    

    【讨论】:

    • 我发现的最好最简单的
    【解决方案3】:

    使用min-height 代替 height 使其高度可变。

    https://codepen.io/hunzaboy/pen/prxgRb

    同样在较小的屏幕上,只需删除绝对位置并使其成为静态。

    【讨论】:

    • 这可行,但不是“要走的路”,因为 BS4 现在是 flexbox。
    猜你喜欢
    • 2013-05-31
    • 2013-04-18
    • 2018-03-25
    • 2014-09-09
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 2020-05-26
    相关资源
    最近更新 更多