CSS

Flexible Sticky Footer with Flexbox

Implement a sticky footer that consistently stays at the bottom of the viewport, even with minimal content, using the powerful capabilities of CSS Flexbox.

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column; /* Stacks children vertically */
}

.main-content {
  flex: 1; /* This pushes the footer down to the bottom */
  padding: 20px;
}

.footer {
  padding: 20px;
  background-color: #333;
  color: white;
  text-align: center;
  flex-shrink: 0; /* Prevents footer from shrinking if content is too large */
}
How it works: This snippet creates a 'sticky footer' that always rests at the bottom of the viewport. By setting `html` and `body` to `height: 100%` and `body` to `display: flex` with `flex-direction: column`, the `.main-content` element can be given `flex: 1`. This property allows `main-content` to grow and consume all available vertical space, effectively pushing the `.footer` element to the very bottom of the page, regardless of how much content is present.

Need help integrating this into your project?

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

Hire DigitalCodeLabs