2 Ways to Build a Sticky Footer (Flexbox and CSS Grid)
As CSS evolves rapidly with exciting new features, common layout tasks are made easier and easier. To prove it, in this short tutorial, we’ll cover two quick ways to create a sticky footer—a component that will sit at the page’s bottom no matter the page’s content height.
Our Sticky Footers



Check the first version of our page that uses Flexbox for the layout (full page demo):
And here's its second version with CSS Grid (full page demo):
Be sure to view the demos from a tall screen and pay attention to how the footer sticks to the bottom of the page. You can also try to insert more content by hitting the Add Content button.
The Page Markup
The page markup will be pretty typical: a wrapper element that will include the page header, the page content, and the page footer like this:
1 |
<div class="site-wrapper"> |
2 |
<header class="site-header">...</header> |
3 |
<main class="site-main">...</main> |
4 |
<footer class="site-footer">...</footer> |
5 |
</div>
|
Of course, the body element could also work as the page wrapper, but I always prefer to leave this element with basic styling.
Our footer will include some widgets. Although we won’t discuss anything about them here, it’s worth noting that in an upcoming tutorial, we’ll use this demo and learn two ways to make the footer widgets dynamic with WordPress.
Sticky Footer With Flexbox
Using flexbox for a sticky footer requires the following actions:
- We’ll make the page wrapper a flex container and give it a minimum height equal to the viewport height.
- We’ll stack the flex items vertically—from top to bottom.
- The
mainelement will expand to cover the available space inside the flex container. To do it, we’ll give itflex-grow: 1. Remember that, by default, all flex items haveflex-grow: 0. - Optionally to vertically center the contents inside the
mainelement, we’ll also set it as a flex container and give italign-items: center.
Here are the corresponding styles:
1 |
.site-wrapper { |
2 |
display: flex; |
3 |
flex-direction: column; |
4 |
min-height: 100vh; |
5 |
}
|
6 |
|
7 |
.site-main { |
8 |
display: flex; |
9 |
align-items: center; |
10 |
flex-grow: 1; |
11 |
}
|
12 |
|
13 |
/*AUTO MARGINS - INSTEAD OF align-items: center*/
|
14 |
.site-main > * { |
15 |
margin-top: auto; |
16 |
margin-bottom: auto; |
17 |
}
|
Sticky Footer With CSS Grid
Using CSS Grid for a sticky footer requires the following actions:
- We’ll make the page wrapper a grid container and give it a minimum height equal to the viewport height.
- We’ll define the grid rows and their size using
grid-template-rows: auto 1fr auto. That means that the page header and footer will be as tall as their content, while themainelement will have a height equal to1fr, so it’ll grow to occupy the empty vertical space. - Optionally to vertically center the contents inside the
mainelement, we’ll set it as a flex container and give italign-items: center.
Here are the corresponding styles:
1 |
.site-wrapper { |
2 |
display: grid; |
3 |
grid-template-rows: auto 1fr auto; |
4 |
min-height: 100vh; |
5 |
}
|
6 |
|
7 |
.site-main { |
8 |
display: flex; |
9 |
align-items: center; |
10 |
}
|
Conclusion
In this tutorial, we examined two ways of building a sticky (fixed) footer. The power of modern CSS allows us to create things like this with minimal effort and without polluting the markup with unnecessary stuff.
As discussed, in a newer tutorial we build on this demo and learn two methods to make the footer widgets dynamic in WordPress. Take a look!
As always, thanks a lot for reading!
Flexbox/CSS Grid Tutorials on Tuts+
Interested in more Flexbox and CSS Grid tutorials? Check out these resources below!


2 Ways to Build a Scrolling Card UI (Flexbox and CSS Grid)

George Martsoukos14 Jul 2022

How to Build a Full-Screen Responsive Page With Flexbox

George Martsoukos20 Nov 2018

CSS Grid vs. Flexbox: Which Should You Use and When?

Anna Monus01 Jan 2024

How to Build a Responsive, Multi-Level, Sticky Footer With Flexbox

Anna Monus05 Jun 2019

How to Build Seamless Masonry Layouts With CSS Grid and object-fit:cover

George Martsoukos21 Jul 2022




