CSS
Build a Sticky Footer Layout with CSS Flexbox
Implement a classic sticky footer design ensuring the footer always stays at the bottom of the viewport, even with sparse content, using Flexbox for robust layout management.
body {
display: flex;
flex-direction: column;
min-height: 100vh; /* Ensure body takes full viewport height */
margin: 0;
font-family: sans-serif;
}
header {
background-color: #333;
color: white;
padding: 1em;
text-align: center;
}
main {
flex-grow: 1; /* This makes the main content push the footer down */
padding: 1em;
}
footer {
background-color: #eee;
padding: 1em;
text-align: center;
}
How it works: This snippet creates a 'sticky footer' layout where the footer remains at the bottom of the viewport, even if the main content area is short. By setting `display: flex` and `flex-direction: column` on the `body`, and then `flex-grow: 1` on the `main` element, the main content area expands to fill all available vertical space, pushing the footer down. `min-height: 100vh` on the body ensures it occupies at least the full viewport height.