What I wanted was to have the footer be at the bottom of browser view ONLY IF the content was not long enough to fill up the browser window (non-sticky).
I was able to achieve by using the CSS function calc(). Which is supported by all modern browsers. You could use like:
I tried: http://jsfiddle.net/AU6yD/ as here it's the best option but I had problems when the content of the <div id="wrapper"> started to get too big see
evidence.png, what I did to solve this was refreshing the body size everytime I changed the content of that div like this:
var body = document.body,
html = document.documentElement;
body.style.height = 100 + "%";
setTimeout(function(){
var height = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight,
html.offsetHeight );
body.style.height = height + "px";
}, 500);
To make it work responsively when the footer is taller on mobile devices compare to on desktop, you can't really know the footer height. One way to do it is to stretch the footer out to cover the entire screen.
Reworking the jQuery solution. I have it working with resizing the window. When the window is bigger than the page, the footer style's position is "absolute" fixed at the bottom the window. When the window is smaller than the page, the footer stays at the bottom of the page - scrolling off the bottom.
<style>
.FooterBottom {
width:100%;
bottom: 0;
position: absolute;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
FooterPosition();
$(window).resize(function () {
FooterPosition();
});
});
var originalFooterBottom = 0;
function FooterPosition() {
var windowHeight = $(window).height();
if (originalFooterBottom == 0) {
var footer = $("#Footer");
originalFooterBottom = footer.position()['top'] + footer.height();
}
if (windowHeight > originalFooterBottom) {
var footerElement = document.getElementById("Footer");
if (!footerElement.classList.contains('FooterBottom')) {
footerElement.classList.add('FooterBottom');
}
}
else {
var footerElement = document.getElementById("Footer");
if (footerElement.classList.contains('FooterBottom')) {
footerElement.classList.remove('FooterBottom');
}
}
}
</script>
I tried many style only solutions but none of them worked. This Javascript/Style solution works just fine for me, even with its odd mix of jQuery and GetElement.
Your issue can be easily fixed by using flexbox. Just wrap your .container and your .footer in a flex container with a min-height: 100vh, switch the direction to column so that they stack on top of each other, and justify the content with space between so that footer will move to the bottom.
Use a blank div with flex-grow:1 to fill unused spaced right before the footer.
<body style="min-height: 100vh; display: flex; flex-direction: column;">
Any content you want,
put it here. Can be wrapped,
nested, whatever.
<div style="flex-grow:1"></div>
<!-- Any content below this will always be at bottom. -->
<footer>Always way at the bottom</footer>
</body>
Extract the styles to css as needed. This works by setting <body> as display:flex;
Luke's answer only works if you have two elements in the wrapper. It's very common to have at least 3: header, main content and footer. With these three elements, Luke's code will space them evenly vertically - most likely not what you want. With multiple elements, you can do this:
I Found this very simple way, just take the main container add min-height and make the footer sticky
body { /* the main container */
min-height: 100vh;
}
footer {
position: sticky;
top: 100%;
}
There are great answer above, i think the problem is that nowadays most people whant to do this with some framework like React, Next or Vue, these frameworks add another element to wrap all the html rendered by the framework like divs with an id #root #__next
so we have to aply the style to that element
In my website i have 3 seperation divs div-header,div-body,div-footer. i put
min-height for div-body using javascript(i used window.height()) . so when content is low then it will maintain minimum height.so that the footer always be in bottom.
I just wanted to add that I had to use this variation min-height: calc(100vh - "any margin top/bottom of any sibling element above the footer"); to make the footer really sticky at the bottom when the page have not enough content and avoiding unnecessary scrolling.