How to Make Footer Stay at Bottom in CSS - Simple Sticky Footer
flexbox on the container with min-height: 100vh and set the main content to grow with flex: 1. This pushes the footer down even if the page content is short, keeping it at the bottom.Syntax
Use a container with display: flex; and flex-direction: column; to stack content vertically. Set min-height: 100vh; so the container fills the full viewport height. Then, make the main content area flexible with flex: 1; to take all available space, pushing the footer to the bottom.
html, body {
height: 100%;
margin: 0;
}
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.main {
flex: 1;
}
.footer {
background-color: #ccc;
padding: 1rem;
text-align: center;
}Example
This example shows a page with a header, main content, and a footer that stays at the bottom even if the main content is short.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Sticky Footer Example</title> <style> html, body { height: 100%; margin: 0; } .container { display: flex; flex-direction: column; min-height: 100vh; } header { background: #4CAF50; color: white; padding: 1rem; text-align: center; } .main { flex: 1; padding: 1rem; background: #f9f9f9; } footer { background: #333; color: white; padding: 1rem; text-align: center; } </style> </head> <body> <div class="container"> <header>Header</header> <main class="main"> <p>This is the main content area.</p> <p>Footer stays at the bottom even if content is short.</p> </main> <footer>Footer</footer> </div> </body> </html>
Common Pitfalls
One common mistake is not setting the container's height or min-height to 100% or 100vh, so the footer floats up when content is short. Another is forgetting to make the main content flexible with flex: 1;, which is needed to push the footer down.
Also, avoid using fixed positioning for the footer unless you want it always visible on screen, which can cover content.
/* Wrong way: footer floats up when content is short */ .container { display: flex; flex-direction: column; /* missing min-height: 100vh; */ } .main { /* missing flex: 1; */ } /* Right way: */ .container { display: flex; flex-direction: column; min-height: 100vh; } .main { flex: 1; }
Quick Reference
- Use
display: flex;andflex-direction: column;on the page container. - Set
min-height: 100vh;on the container to fill the viewport height. - Make the main content flexible with
flex: 1;to push the footer down. - Do not use fixed positioning unless you want the footer always visible.