Skip to content

Instantly share code, notes, and snippets.

@RSpeekenbrink
Last active February 5, 2019 14:00
Show Gist options
  • Save RSpeekenbrink/062bb69504738a3ad156a021dedb2418 to your computer and use it in GitHub Desktop.
Save RSpeekenbrink/062bb69504738a3ad156a021dedb2418 to your computer and use it in GitHub Desktop.
Fix the footer to the bottom of the page if the lack of content on the page causes white space under the footer.
/**
* Set the footer (<footer>) location to the bottom of the screen depending on screen size and content on the page.
*/
window.addEventListener('load', FooterToPageBottom); window.addEventListener('resize', FooterToPageBottom);
function FooterToPageBottom() {
var footer = document.getElementsByTagName("footer"); //get <footer> elements here (Get elementById to make it slightly faster ;) )
if(footer.length < 1) return; //does a <footer> element exist?
footer = footer[0]; //grab first footer element to work with :D
footer.style.position = ""; footer.style.bottom = ""; //Reset footer style
if(((window.innerHeight - footer.offsetHeight) < footer.offsetTop)) return; //If no whitespace under footer do nothing :) #Magic
footer.style.width = "100%"; footer.style.position = "absolute"; footer.style.bottom = "0"; //Fix footer pos
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment