Explain Codes LogoExplain Codes Logo

Bootstrap 3 Flush footer to bottom, not fixed

html
footer
flexbox
css
Nikita BarsukovbyNikita Barsukov·Aug 9, 2024
TLDR

The Flexbox model provides a straightforward solution for sticking a footer to the bottom, without making it fixed. The trick is to use display: flex and flex-direction: column on the body, along with setting flex-grow: 1 on the main content, ensuring it takes up all available space and effectively pushing the footer to the bottom of the viewport.

<body style="display: flex; flex-direction: column; min-height: 100vh;"> <main style="flex-grow: 1;"> <!-- Main content goes here --> </main> <footer> <!-- Footer content goes here --> </footer> </body>

Detailed strategy for non-fixed footers

Sometimes, you may need to resort to good ol' absolute positioning to get that footer to behave. In such cases, ensure you set the margin-bottom on your wrapper to match the height of your footer.

#wrapper { margin-bottom: -50px; /* This value should match the footer height */ } footer { position: absolute; bottom: 0; height: 50px; /* Boom, footer's height: No more, no less */ }

Maximizing adaptability with CSS flex containers

Dealing with pages filled to the brim with content? No fear, dynamic content adjustments are here! By using display: flex in your .container class, the layout remains consistent whether you've got a novella or a tweet's worth of content.

.container { display: flex; flex-wrap: wrap; }

With flex containers, your footer becomes as flexible as a world-class gymnast, bending and stretching to accommodate incoming content without overlapping.

Pro tip: min-height is your friend. Use vh units to gracefully handle instances with lighter content:

footer { min-height: 10vh; /* Adjust this value to fit your aesthetic preference */ }

Now your webpage looks balanced and well-designed, no matter the content length, and the footer stylishly fills up space without looking ridiculously large.

Advanced concepts and practical tricks

Say goodbye to fixed footers

Throw out fixed-height footers when dealing with dynamic content. Embrace flexbox to make sure the footer knows when to do a little limbo and duck under incoming content.

body { display: flex; min-height: 100vh; flex-direction: column; } main { flex-grow: 1; /* "I must grow," said the main content. */ }

Looking good with inverted colors

Want a dashing 007 look for your footer? Try out the navbar-inverse class in Bootstrap for an instant makeover.

< footer class="navbar-inverse"> <!-- The footer is in disguise --> <!-- Footer content --> </footer>

Practical implementation

Always refer to working examples when implementing these strategies. They've been battle-tested in many a page layout skirmish, leaving the Box Model battlefield victorious.