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.
Check the first version of our page that uses Flexbox for the layout (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.
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
main
element 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
main
element, we’ll also set it as a flex container and give italign-items: center
.
Another way to vertically center the flex items is with auto margins.
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 |
}
|
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 themain
element will have a height equal to1fr
, so it’ll grow to occupy the empty vertical space. - Optionally to vertically center the contents inside the
main
element, we’ll set it as a flex container and give italign-items: center
.
Both the flex-grow property and the fr CSS unit work similarly. The former distributes the remaining space inside the container among the flex items, while the latter distributes the remaining space inside the container among the grid rows/columns. One key difference is that we set the flex-grow property to the flex items while the fr unit to the grid containers.
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 an upcoming tutorial we’ll build on this demo and learn two methods to make the footer widgets dynamic in WordPress. Stay tuned!
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!