Course contentsShow
← Web Development
Lesson 297 of 2,9825. FlexboxFree lesson

Sticky Footer Pattern

Use flexbox to push footer to bottom even with minimal content.

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:

  1. Make the body a flex container with display: flex and flex-direction: column to stack elements vertically
  2. Give your main content area flex-grow: 1 so it expands to fill available space
  3. 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.