Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep footer at the bottom even with dynamic height website

Tags:

How do I keep a footer div always at the bottom of the window when I have a page that dynamically set the height (get info from database, for example) with CSS?


If you want to do with jQuery, i came up with this and works fine:

Set the CSS of your footer:

#footer { position:absolute; width:100%; height:100px; } 

Set the script:

<script>   x = $('#div-that-increase-height').height()+20; // +20 gives space between div and footer   y = $(window).height();       if (x+100<=y){ // 100 is the height of your footer     $('#footer').css('top', y-100+'px');// again 100 is the height of your footer     $('#footer').css('display', 'block');   }else{     $('#footer').css('top', x+'px');     $('#footer').css('display', 'block');   } </script> 

This script must be at the end of your code;

like image 800
Rafael Fonseca Avatar asked Jan 11 '12 07:01

Rafael Fonseca


People also ask

How do I keep the footer at the bottom of the page with dynamic height?

If you wrap the content in a tag and set the margin-bottom value to the same as the footer-height, you would be able to read the text that the footer was originally covering.

How do I keep a website footer at the bottom?

To make a footer fixed at the bottom of the webpage, you could use position: fixed. < div id = "footer" >This is a footer. This stays at the bottom of the page.

How do I stick footer to the bottom when page content is less?

Quick answer: Add “display:flex; flex-direction:column; min-height:100vh;” to body or appropriate layout body element, then add “flex:1;” to content wrapper element/section. I will explain few ways to keep or stick the footer to the bottom of the page if there is not enough content.


2 Answers

I think this will solve all your problems:

    <script>    $(document).ready(function() {     var docHeight = $(window).height();    var footerHeight = $('#footer').height();    var footerTop = $('#footer').position().top + footerHeight;     if (footerTop < docHeight) {     $('#footer').css('margin-top', 10+ (docHeight - footerTop) + 'px');    }   });  </script> 

You need at least an element with a #footer

When not want the scrollbar if content would fit to screen just change the value of 10 to 0 The scrollbar will show up if content not fits to screen.

like image 84
HenryW Avatar answered Oct 09 '22 07:10

HenryW


I believe you are looking for a Sticky Footer

Try this: https://web.archive.org/web/20161117191810/http://ryanfait.com/sticky-footer/ (archive)

From the article above:

layout.css:

* {     margin: 0; } html, body {     height: 100%; } .wrapper {     min-height: 100%;     height: auto !important;     height: 100%;     margin: 0 auto -142px; /* the bottom margin is the negative value of the footer's height */ } .footer, .push {     height: 142px; /* .push must be the same height as .footer */ }  /*  Sticky Footer by Ryan Fait http://ryanfait.com/  */ 

The html page:

<html>     <head>         <link rel="stylesheet" href="layout.css" ... />     </head>     <body>         <div class="wrapper">             <p>Your website content here.</p>             <div class="push"></div>         </div>         <div class="footer">             <p>Copyright (c) 2008</p>         </div>     </body> </html> 
like image 23
agarcian Avatar answered Oct 09 '22 07:10

agarcian