我知道这个问题可能被问了几百次,但遗憾的是我在这里找到的答案并没有真正帮助我。
例如,我尝试了这些答案:
但是我仍然有一个问题,当我的页面内容“小”并且没有填满主体/页面容器的整个高度时,页脚只是浮动在浏览器窗口末端上方的某个地方。
这是我的页脚的代码:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
<footer class="d-flex justify-content-center">
<div class="d-flex justify-content-between col-md-8 col-md-offset-2 mb-3 mt-5">
<div class="align-left">
<a class="font-weight-bold small kf-blue kf-links" href="#">Link1</a> |
<a class="font-weight-bold small kf-blue kf-links" href="/">Link2</a> |
<a class="font-weight-bold small kf-blue kf-links" href="/">Link3</a>
</div>
<div class="align-right small">
Crafted with Love by <a class="font-weight-bold kf-blue kf-links" href="#" target="_blank">Me</a>
</div>
</div>
</footer>
我正在使用 Bootstrap 4.1 和 Chrome,这也是我网站的代码笔:
https://codepen.io/anon/pen/oMZVxq
注意:您必须使用 codepen 中的侧边栏视图才能真正看到页脚不在底部,因为 codepen 中的视图尺寸太小以至于看起来正确。
原文由 NakedPython 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用内置的引导程序类来实现这一点。
你需要的是容器成为一个 column flex container 。要使用的类是:
d-flex flex-column
要将容器设置为高度:100%,您可以将类
h-100
应用于 html、正文和容器或添加到容器样式height:100vh;
对于页脚,一个
margin-top:auto
就可以了,要使用的类是:mt-auto
;下面的示例以全页模式运行
codepen 更新 https://codepen.io/anon/pen/PBpgNN
boostrap 类的提醒 https://getbootstrap.com/docs/4.5/utilities/flex/ 关于调整大小请参阅 https://getbootstrap.com/docs/4.5/utilities/sizing/