Sticky Footers In HTML CSS (Very Simple Examples)

Welcome to a tutorial on how to create sticky footers in HTML and CSS. So you want to stick a navigation bar, or fix a banner at the bottom of the page?

One of the easiest ways to create a sticky footer is to set CSS position: sticky on the footer.

  • <footer style="position:sticky; bottom:0;">FOOT</footer>

That covers the quick basics, but read on for more methods and examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Sticky Footer Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

HTML CSS STICKY FOOTERS

All right, let us now get into the various ways to create sticky footers in HTML and CSS.

 

1) FIXED FOOTER

1A) THE DEMO

My Site
Contents
Contents
Page Footer

 

1B) THE HTML

1-fixed.html
<!-- (A) PAGE HEADER -->
<header id="page-head">My Site</header>
 
<!-- (B) MAIN CONTENTS -->
<main id="page-body">
  <p>Contents</p>
  <p style="height:3000px"></p>
  <p>Contents</p>
</main>
 
<!-- (C) FOOTER -->
<footer id="page-foot">Page Footer</footer>

This 3 sections page should be self-explanatory – Header on top, contents in the middle, and a footer at the bottom.

 

1C) THE CSS

1-fixed.css
/* (A) FOOTER */
#page-foot {
  /* (A1) FIX AT BOTTOM */
  position: fixed;
  bottom: 0;
  z-index: 9;
 
  /* (A2) DIMENSIONS */
  width: 100%;
  height: 40px;
  padding: 10px;
 
  /* (A3) CENTER */
  display: flex;
  align-items: center;
}
 
/* (B) PREVENT BOTTOM CONTENT FROM BEING COVERED */
#page-body {
  padding: 10px 10px 40px 10px;
}
  1. To keep the footer at the bottom of the page permanently, we simply have to set position: fixed; bottom: 0;
  2. But take note that a fixed footer will cover the bottom portion of the main contents. To prevent that from happening, we give the body more padding at the bottom – padding: 10px 10px 40px 10px.

 

 

2) STICKY FOOTER

2A) THE DEMO

My Site
Contents
Contents
Bottom Navigation
Page Footer

 

2B) THE HTML

2-sticky.html
<!-- (A) PAGE HEADER -->
<header id="page-head">My Site</header>
 
<!-- (B) MAIN CONTENTS -->
<main id="page-body">
  <p>Contents</p>
  <p style="height:3000px"></p>
  <p>Contents</p>
</main>
 
<!-- (C) FOOTER -->
<nav id="page-foot-nav">Bottom Navigation</nav>
<footer id="page-foot">Page Footer</footer>

This is pretty much the same layout as the previous example, but it has one more bottom navbar before the footer that we need to stick.

 

2C) THE CSS

2-sticky.css
/* (A) STICKY FOOTER NAV */
#page-foot-nav {
  position: sticky;
  bottom: 0;
  z-index: 9;
}

Yep, position: sticky; bottom: 0; does all the magic.

 

 

3) BOTTOM FOOTER WITH CSS FLEX

3A) THE DEMO

My Site
Contents
Page Footer

 

3B) THE HTML

3-flex.html
<!-- (A) PAGE HEADER -->
<header id="page-head">My Site</header>
 
<!-- (B) MAIN CONTENTS -->
<main id="page-body">
  <p>Contents</p>
  <!--
  <p style="height:3000px"></p>
  <p>Contents</p>
  -->
</main>
 
<!-- (C) FOOTER -->
<footer id="page-foot">Page Footer</footer>

Look no further this is the same as the first example again. But in this one, let us assume that we don’t have enough contents to fill the full height of the page… It just looks very ugly with the footer floating in the middle of the page.

 

3C) THE CSS

3-flex.css
/* (A) BODY */
body {
  /* (A1) FLEX COLUMN */
  display: flex;
  flex-direction: column;
 
  /* (A2) AT LEAST WINDOW HEIGHT */
  min-height: 100vh;
}
 
/* (B) CONTENT */
#page-body { flex-grow: 1; }

To “force” the footer to the bottom:

  • body { display: flex; flex-direction: column; } Will layout the header, body, footer in a single column.
  • min-height: 100vh Makes sure that the <body> will at least cover the height of the entire window.
  • #page-body { flex-grow: 1 } allows the contents area to “grow”, effectively “force” the footer to the bottom.

 

4) BOTTOM FOOTER WITH CSS GRID

4A) THE DEMO

My Site
Contents
Page Footer

 

4B) THE HTML

4-grid.html
<!-- (A) PAGE HEADER -->
<header id="page-head">My Site</header>
 
<!-- (B) MAIN CONTENTS -->
<main id="page-body">
  <p>Contents</p>
  <!--
  <p style="height:3000px"></p>
  <p>Contents</p>
  -->
</main>
 
<!-- (C) FOOTER -->
<footer id="page-foot">Page Footer</footer>

Yes, the layout is still the same for this final example, with the same “ugly footer in middle of the screen” problem.

 

4C) THE CSS

4-grid.css
/* (A) BODY */
body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

Instead of display: flex, we are using another CSS3 goodie to solve the problem – display: grid. Very simply:

  • min-height: 100vh Ensures <body> will at least cover the full height of the window once again.
  • grid-template-rows: auto 1fr auto does the layout magic. In “English” – Set the header and footer to auto height, but allow the contents to “grow to fill”.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

COMPATIBILITY CHECKS

All the required CSS features are well-supported on all modern browsers.

 

LINKS & REFERENCES

 

INFOGRAPHICS CHEAT SHEET

HTML CSS Sticky Footer (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.