Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS to make HTML page footer stay at bottom of the page with a minimum height, but not overlap the page

Tags:

css

footer

I have the following page (deadlink: http://www.workingstorage.com/Sample.htm ) that has a footer which I can't make sit at the bottom of the page.

I want the footer to

  • stick to the window bottom when the page is short and the screen is not filled, and
  • stay at the document end and move down as normal when there is more than a screenful of content (instead of overlapping the content).

The CSS is inherited and befuddles me. I can't seem to change it properly to put a minimum height on the content or make the footer go to the bottom.

like image 986
Caveatrob Avatar asked Mar 13 '09 17:03

Caveatrob


People also ask

How do I make the footer stay at the bottom HTML CSS?

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 get the footer div to stay at the bottom of the page?

The trick is to use a display:table for the whole document and display:table-row with height:0 for the footer. Since the footer is the only body child that has a display as table-row , it is rendered 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.

How do you keep the footer at the bottom even with dynamic height?

Just add data-position="fixed" to the div tag if you are using jQuery. Hope this helps.


2 Answers

Below are 4 different methods of mine:

In each example the texts are freely-editable to illustrate how the content would render in different scenarios.


1) Flexbox

body{ height:100vh; margin:0; }  header{ min-height:50px; background:lightcyan; } footer{ min-height:50px; background:PapayaWhip; }  /* Trick */ body{    display:flex;    flex-direction:column;  }  footer{   margin-top:auto;  }
<body>   <header contentEditable>Header</header>   <article contentEditable>Content</article>   <footer contentEditable>Footer</footer> </body>

2) Grid

body{    min-height: 100vh;    margin: 0;       display: grid;   grid-template-rows: auto 1fr auto; }  header{    min-height:50px;   background:lightcyan;  }  footer{    min-height:50px;    background:PapayaWhip;  }
<body>   <header contentEditable>Header</header>   <article contentEditable>Content</article>   <footer contentEditable>Footer</footer> </body>

This method below uses a "trick" by placing an ::after pseudo-element on the body, and set it to have the exact height of the footer, so it will occupy the exact same space the footer does, so when the footer is absolute positioned over it, it would appear like the footer is really taking up space and eliminate the negative affects of it's absolute positioning (for example, going over the body's content)

3) position:absolute (no dynamic footer height)

body{ min-height:100vh; margin:0; position:relative; } header{ min-height:50px; background:lightcyan; } footer{ background:PapayaWhip; }  /* Trick: */ body {   position: relative; }  body::after {   content: '';   display: block;   height: 50px; /* Set same as footer's height */ }  footer {   position: absolute;   bottom: 0;   width: 100%;   height: 50px; }
<body>   <header contentEditable>Header</header>   <article contentEditable>Content</article>   <footer contentEditable>Footer</footer> </body>

4) Table-layout

html{ height:100%; } body { min-height:100%;  margin:0; }  header {   height: 50px;   background: lightcyan; }  article {    height: 1%; }  footer {   height: 50px;   background: PapayaWhip; }  /**** Trick: ****/  body {   display: table;   width: 100%;  }  body > footer {    display: table-row; }
<body>   <header contentEditable>Header</header>   <article contentEditable>Content</article>   <footer contentEditable>Footer</footer> </body>
like image 155
vsync Avatar answered Nov 19 '22 17:11

vsync


A simple method is to make the body 100% of your page, with a min-height of 100% too. This works fine if the height of your footer does not change.

Give the footer a negative margin-top:

footer {     clear: both;     position: relative;     height: 200px;     margin-top: -200px; } 
like image 33
Jon Winstanley Avatar answered Nov 19 '22 17:11

Jon Winstanley