CSS
Flexible Sticky Footer Layout with Flexbox
Build a classic sticky footer layout ensuring the footer always rests at the bottom of the viewport, even with minimal content, and pushes down with more content.
html, body {
height: 100%;
margin: 0;
}
body {
display: flex;
flex-direction: column;
}
.content-wrap {
flex: 1 0 auto; /* Grows to fill available space, never shrinks below content */
}
.site-footer {
flex-shrink: 0; /* Prevents footer from shrinking */
padding: 1rem;
background-color: #f0f0f0;
text-align: center;
}
How it works: This Flexbox solution creates a sticky footer. By setting `display: flex;` and `flex-direction: column;` on the `body`, content flows vertically. `height: 100%;` on `html, body` ensures the body takes full viewport height. The `.content-wrap` (main content area) uses `flex: 1 0 auto;` to `flex-grow` and fill any available space, pushing the footer down. The `.site-footer` has `flex-shrink: 0;` to prevent it from shrinking when space is limited, ensuring it always maintains its intended height.