CSS Snippet

Sticky Footer with Flexbox

Difficulty: Medium

A footer that stays at the bottom of the viewport on short pages and at the bottom of the content on long pages used to require absolute positioning, fixed heights, or table-cell tricks. Flexbox solves it with two declarations. This snippet covers the canonical body-flex layout, the grid-based equivalent, and the wrapper variant for apps that already have a top-level container.

Code Snippets
/

Sticky Footer with Flexbox

Sticky Footer with Flexbox

A footer that stays at the bottom of the viewport on short pages and at the bottom of the content on long pages used to require absolute positioning, fixed heights, or table-cell tricks. Flexbox solves it with two declarations. This snippet covers the canonical body-flex layout, the grid-based equivalent, and the wrapper variant for apps that already have a top-level container.

CSS
Medium
3 snippets
css-selectors
css-flexbox
css-grid
code-template

505 views

7

Setting body to display: flex; flex-direction: column; min-height: 100vh makes the body at least the viewport tall and stacks its children vertically. main gets flex: 1, which means it expands to consume any leftover space, pushing the footer to the bottom on short pages. On long pages the footer sits naturally below the content because flex: 1 does not force the main to be larger than its content. The min-height: 100vh (instead of height: 100vh) is what allows the page to grow past the viewport when content overflows.