I know this question was probably asked some hundred times, but sadly no answer that I found in here really helped me.
I tried these answers for example:
- Bootstrap footer not at bottom
- Flushing footer to bottom of the page, twitter bootstrap
- Height not 100% on Container Fluid even though html and body are
But I still have the problem that when my page content is to “small” and doesn’t fill out the whole height of the body/page container the footer just floats somewhere above the end of the browser window.
This is the code for my footer:
<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>
I’m using Bootstrap 4.1 and Chrome, here’s also a codepen to my site:
https://codepen.io/anon/pen/oMZVxq
Note: you have to use the sidebar view in codepen to actually see that the footer is not at the bottom, as the view size in codepen is so small that it looks correctly.
4
Answers
First add
display: flex;
andflex-direction: column;
to#page-container
.Now you have “set the stage” for aligning the footer to the bottom. Set its
margin-top
toauto
(by adding the classmt-auto
) and you are done;See this codepen.
You can use built-in bootstrap class to achieve this.
What you need is the container to be a column flex container . class to use are :
d-flex flex-column
To set the container to height:100% you can apply the class
h-100
to html, body and the container or add to the container styleheight:100vh;
For the footer, a
margin-top:auto
will do, the class to use is :mt-auto
;example below to run in full page mode
codepen updated https://codepen.io/anon/pen/PBpgNN
reminder for boostrap classes https://getbootstrap.com/docs/4.5/utilities/flex/ about sizing see https://getbootstrap.com/docs/4.5/utilities/sizing/
If you want a fixed footer, just add the class
fixed-bottom
to thefooter
tag like shown below.So this helped me – if someone is still looking for an answer:
on your
<HTML>
,<body>
and your container div add a classh-100
and your footer will stay on the bottom.Important
One thing I did to remove the extra height is that on my container div I changed the
h-100
styling toheight: calc(100% - 200px) !important;
(where -200px was the height of my navigation and footer)if you are not using the bootstrap, here is the styling
.h-100{height:100% !important}