CSS

Flexbox: Creating a Sticky Footer Layout (Full Page)

Implement a "sticky footer" that always stays at the bottom of the viewport, even if the main content is short, and pushes down if content expands, using CSS Flexbox for robust page layouts.

html, body {
  height: 100%; /* Important: html and body must take full height */
  margin: 0;
}

.page-wrapper {
  display: flex;
  flex-direction: column; /* Stack items vertically */
  min-height: 100vh; /* Ensure wrapper takes full viewport height */
}

.header {
  background-color: #4a148c;
  color: white;
  padding: 15px;
  text-align: center;
}

.main-content {
  flex-grow: 1; /* This is the key: main content takes all available space */
  background-color: #f3e5f5;
  padding: 20px;
}

.footer {
  background-color: #4a148c;
  color: white;
  padding: 15px;
  text-align: center;
}

/* Example content to demonstrate stickiness */
.short-content {
  /* This content will ensure the main-content area doesn't push beyond footer */
  min-height: 100px;
  border: 1px dashed #7b1fa2;
  padding: 10px;
  margin-bottom: 10px;
}

.long-content {
  /* Example for when content overflows */
  height: 1200px; /* Make content artificially long */
  background-color: #e0f2f7;
  padding: 20px;
  overflow-y: auto;
  border: 1px solid #01579b;
}

/* HTML Structure */
/*
<div class="page-wrapper">
  <header class="header">Page Header</header>
  <main class="main-content">
    <h1>Welcome!</h1>
    <p class="short-content">This is some short content. The footer will stick to the bottom of the viewport regardless of how much (or little) content is here.</p>
    <p>Scroll down to see the footer (which is at the very bottom).</p>
  </main>
  <footer class="footer">Page Footer © 2023</footer>
</div>

<!-- Scenario 2: Long content, footer pushed down -->
<!-- (You would typically only have one page-wrapper per page. -->
<!-- For demonstration purposes, assume this is a separate page/component) -->
<!--
<div class="page-wrapper">
  <header class="header">Page Header (Long Content)</header>
  <main class="main-content">
    <h1>A Page with Lots of Content</h1>
    <div class="long-content">
      <p>This content is intentionally very long to demonstrate that the footer will be pushed down below the content and not stick to the viewport bottom.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      <p>... (imagine many more paragraphs) ...</p>
      <p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.</p>
    </div>
  </main>
  <footer class="footer">Page Footer © 2023</footer>
</div>
-->
How it works: This snippet provides a robust "sticky footer" solution using Flexbox. The `html` and `body` elements are set to `height: 100%`, and the `.page-wrapper` is a flex container with `flex-direction: column` and `min-height: 100vh`. The crucial part is `flex-grow: 1` applied to the `.main-content`. This tells the main content area to take up all available space between the header and footer, pushing the footer to the bottom of the viewport when content is short, and allowing it to be pushed further down when content expands beyond the viewport height.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs