What You'll Learn
You'll learn how to use flexbox to make a footer stick to the bottom of the page, even when there's very little content to fill the screen.
The Sticky Footer Problem
Imagine a webpage with just a heading and one paragraph. Without intervention, the footer sits awkwardly in the middle of the screen, leaving a large white gap below it. Users expect footers to stay at the bottom—either naturally (when there's lots of content) or forcefully (when content is minimal).
The Flexbox Solution
The sticky footer pattern uses flexbox to push the footer down automatically:
- Make the body a flex container with
display: flexandflex-direction: columnto stack elements vertically - Give your main content area
flex-grow: 1so it expands to fill available space - The header and footer remain their natural size, while the main content stretches
Here's the structure:
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex-grow: 1;
}
The min-height: 100vh ensures the body is at least as tall as the viewport. The main content area grows (flex-grow: 1) to consume any leftover space, pushing the footer down. If content exceeds the viewport height, everything flows naturally—the footer simply appears after all content.
Think of it like three people sharing a blanket: the header and footer take what they need, and the middle person (main content) stretches to use whatever's left.
Key Takeaway: The sticky footer pattern uses display: flex, flex-direction: column, and flex-grow: 1 on the main content to ensure footers always sit at the bottom, regardless of content length.