I have a function which pushes my footer to the bottom of the page. It works by adding a margin to the top of the footer, however, i want to add a bg color to my content and with the margin this just leaves a big whitespace in the middle.
Can I extend the div above instead of adding the margin to the footer so that my bg color covers the page with no whitespace.
if ($('#footer').length) {
var doc_height = $(window).height();
var footer_top = $('#footer').position().top + $('#footer').height();
if (footer_top < doc_height) {
$('#footer').css('margin-top', (doc_height - footer_top) + 'px');
}
}
#main_content {
background-color: #f00
}
#footer {
background-color: #0f0
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="main_content">Some text</div>
<div id="footer">Some text</div>