Make footer stick to bottom of page correctly
Cornerstone to your footer's bottom placement comes from harnessing the strength of CSS Flexbox. Spread display: flex
, flex-direction: column
, and min-height: 100vh
on the main container
. This ensures it reaches full viewport height. Setflex: 1
to content
to push footer down when content is light. Take a look at the code:
The HTML gos as follows:
Enjoy your rock-solid foundation, your footer is now immoveable regardless of content quantity. ๐
Crucial concepts explained
Dynamic content and overflow handling
Flexbox might not meet all needs, for instance when dealing with dynamically changing content. Here's an alternative:
- Set
min-height: 100vh
for.container
. Even with ever-changing content, your steady footer remains at the viewport's edge. - Use
overflow: auto;
to clip any protrusions, keeping the footer right where it should be.
Short and fixed pages
On pages with sparse content, footers tend to levitate. Fixing the footer position can calm this AB-normal-behavior:
Align base styles
Consistency is key. Ensure html
and body
share a wardrobe:
Footer margins
Footers may show unhelpful margins. Reset this behavior:
Advanced sightings
Flexible containers & dynamic content
Adapt to wildly varying content by deploying flexible containers:
Within its bounds, you can manage varying content:
Stick with it, or fix it?
Position: sticky;
vs position: fixed;
:
- Sticky keeps the footer at the bottom - within its bounds.
- Fixed glues the footer to the viewport's bottom, regardless of container.
Keep it simple
Steer clear of complex, unneeded solutions. Over-complications can lead to cross-browser problems and harder maintenance.
Was this article helpful?