How to align footer (div) to the bottom of the page?
Nail the footer down to the bottom with CSS Flexbox. Configure your outermost container (e.g., body
) with display: flex; flex-direction: column; min-height: 100vh;
ensuring full viewport height. Apply margin-top: auto;
on the footer, pushing it to the footer of the page.
Set up your main content within main tags, and post the footer just below:
Allocate height: 100%;
for html and body to grasp the full height of the page.
This responsive solution will stick the footer right where you want it, across devices.
Comprehensive understanding: For varying content lengths
Dealing with scrolling content
The method above works flawlessly for content that doesn't exactly fill the viewport, as the footer sits tight at the bottom. But, if you have longer content, the footer floats down, right behind the content, staying put upon scrolling. No freak accidents or unhinged footers running amok here!
Ensuring cross-browser consistency
For web pages, consistency is key, and browsers can be as different as apples and oranges. Imagine handing out slim fit suit to an orangutan! It's sensible to use a CSS reset like normalize.css
to keep all browsers on the same styling page.
Fixed footers for those who hate scrolling
For a footer that stays caught in the viewport during scrolling, manspread position: fixed;
with bottom: 0;
and width: 100%;
. Now the footer stays within reach across the full width of the viewport.
To keep the content from getting camera shy behind the footer, feed padding-bottom
on the body
just enough to match the footer's height.
For footers that can't make up their mind
When the footer is chockfull of context-driven content, give it dimension with height: auto;
to prevent it from overflowing or snipping off content.
Footer placement — Advanced techniques
Embracing Grid Layouts
Shift to Grid layouts for complex footer placement tasks. It amps up the user experience and is as fashionable as tying a bandana around your neck.
Sticky Footers with sticky property
Make your footer sticky with honey... er, I mean position: sticky;
and bottom: 0;
. It will stick to the bottom of its parent block, until it meets the edge of the screen, and then it will start scrolling with the content.
Preventing overlap and enforcing margins
#WatchYourMargins and manage them right with margin-top
and height
to avoid the footer playing tag with the content. Choose negative margins with care, you don't want footers playing hide and seek!
Was this article helpful?