Explain Codes LogoExplain Codes Logo

Footer at bottom of page or content, whichever is lower

html
responsive-design
css-grid
viewport-units
Anton ShumikhinbyAnton ShumikhinΒ·Sep 22, 2024
⚑TLDR

The CSS Flexbox can be the solution to keep your footer always at the right place. Implement your main container with display: flex and flex-direction: column, allowing the content area to expand with flex: 1. This ensures your footer stays at the bottom – either sticking to viewport's bottom or adapting to content height.

html, body { height: 100%; /* Mandatory full-page party! πŸ₯³ */ margin: 0; } #container { display: flex; /* Flex those muscles πŸ’ͺ */ flex-direction: column; /* Vertical orientation, please 🎒 */ min-height: 100%; } #content { flex: 1; /* Content gets all leftover space 🌌 */ } footer { height: 50px; /* A fixed space for footer to chill 😎 */ }

Wrap your content in the #content div and place the footer after it within #container div:

<div id="container"> <div id="content">Your content</div> <footer>Your footer</footer> </div>

We are also applying a box-sizing: border-box rule to ensure that the box size doesn't increase due to padding and borders.

Making footer's height static

Assign a fixed-height to your footer to prevent unintentional layout shifts. The bottom "padding" value for #content should be equal to the footer's defined height:

#content { padding-bottom: 50px; /* Same value as footer height; content and footer respect each other's space ✌️ */ box-sizing: border-box; }

This assures the content doesn't lurk behind the footer, allowing clear separation between them.

In case of dynamically sized footer, utilize Flexbox:

footer { flex-shrink: 0; /* Just like a stubborn mule, the footer won't shrink! 🦾 */ }

This ensures your footer never shrinks regardless of the content area growth or shrinkage. It's a neat little life vest for your footer to stay afloat!

Ensuring 100% container height

Make sure your container scales at least as tall as the viewport:

#container { min-height: 100vh; /* Full height scenario, no hobbit-sized stuff here πŸ§™β€β™‚οΈ */ flex-direction: column; }

This uses viewport units to ensure a full-screen height container regardless of content volume.

An HTML layout with a good structure is paramount for maintenance and readability:

<header>Your header</header> <nav>Your nav</nav> <article>Your content</article> <footer>Your footer</footer>

These tags: header, nav, article, and footer in the same hierarchical level, make a neat structure and maintainability like a well-sorted bookshelf πŸ“š.

CSS Grid can also help manage footer positioning:

body { display: grid; grid-template-rows: 1fr auto; /* 1fr for content because it’s hungry for space, auto for the light-footed footer */ min-height: 100vh; /* Full view ahead! πŸš€ */ }

The 1fr property allows the content to occupy as much space as it may need, keeping the footer planted firm at the end of the page or content.

For a consistent footer across various devices, use media queries:

@media (max-width: 600px) { footer { height: 100px; /* Footer height takes a leap when viewport sneezes! 🀧 */ } #content { padding-bottom: 100px; /* Padding plays catch up! πŸƒβ€β™‚οΈ */ } }

Adjust the footer's height and content’s bottom padding based on the viewport width for a responsive design.

Pro tips for sticky footers

Test your website under different scenarios to make sure your footer behaves as expected with differing content sizes. Regularly check resources like CSS-Tricks for updated methods and new demos to stay tuned with different sticky footer techniques.

Using semantic HTML allows for a simple and accessible structure, and consider vh units for responsiveness.

Creating a working demo

Create an interactive demo using a service like CodePen to showcase your solution practically. This strengthens the understanding of your proposed approach.