<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Sticky Footer Test</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <style> html, body { height: 100%; } body { display: flex; flex-direction: column; min-height: 100vh; } main { flex: 1 0 auto; } footer { flex-shrink: 0; } </style> </head> <body> <header class="bg-primary text-white p-3">Header</header> <main class="container p-3"> <p>Short content here.</p> </main> <footer class="bg-dark text-white p-3">Footer</footer> </body> </html>
The body uses flexbox with column direction and min-height: 100vh. The main area grows to fill available space, pushing the footer to the bottom. The footer does not shrink, so it stays visible at the bottom even if content is short.
body > footer {
box-shadow: 0 -4px 6px rgba(0,0,0,0.1);
}The selector body > footer targets the footer element that is a direct child of the body element. This is the correct way to style the footer in this layout.
Flexbox layouts let the footer stay at the bottom of the page without overlapping content and adjust naturally when content height changes. Absolute positioning can cause overlap and does not adapt well to dynamic content.
flex: 1 0 auto; from the main element in a Bootstrap sticky footer layout?<style>
html, body { height: 100%; }
body { display: flex; flex-direction: column; min-height: 100vh; }
main { /* flex: 1 0 auto; removed */ }
footer { flex-shrink: 0; }
</style>Without flex: 1 0 auto;, the main content does not grow to fill available space, so the footer stays just below content and does not stick to the bottom of the viewport.
Using the semantic <footer> element with role="contentinfo" helps screen readers identify the footer. Ensuring keyboard focus can reach footer links improves navigation for all users.
