Creating a Sticky Footer with Flexbox

David Musk Mar 11, 2016 Read time: 2 minute read
Creating a Sticky Footer with Flexbox

Getting a footer to stick to the bottom of the page with CSS can be problematic. Flexbox is a modern solution for creating sticky footers with variable height.

If you’re a developer, chances are you’ve dealt with footers at some point in your career. Getting them to stick to the bottom of the page can be problematic, especially on pages with sparse content.

Ryan Fait’s sticky footer is the traditional way to solve this problem. This method has served many web developers (myself included) over the years with minimal issues. The only drawback is that it requires a fixed height to function properly.

Fixed pixel-based heights can make updating content needlessly time consuming, especially when we're dealing with different footer sizes on tablet and mobile screens. And what if a client needs the ability to add and remove content dynamically through the CMS? Fixed heights can make that task next to impossible.

Flexbox provides a modern solution that will allow us to create a footer with variable height by wrapping it inside a flex container. The best part? Even if your site is already using the traditional sticky footer method, you only need a few extra lines of CSS to make it work.

The HTML

<body class="site">
  <header>...</header>
  <div class="wrapper">...</div>
  <footer>...</footer>
</body>

The markup here is nothing out of the ordinary. The only thing that stands out is the class on the body. We'll be using that as our flex container. If you'd rather not use the body element, you can wrap your content in another block element like a div or a main instead. Either way is fine.

The CSS

display: flex is used to establish this element as a flex container. flex-direction determines the axis (row for horizontal and column for vertical.) In our case, we'll be using column to fill the space from top to bottom.

.site {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

Once the container is in place, we can have our wrapper div expand to fill the available space. This is what keeps the footer at the bottom of the page, regardless of the page's content.

.wrapper {
  flex:1;
}

And that's all there is to it! Now you can add content to your footer and watch it expand accordingly. It will always stay at the bottom of your page, and you never need to worry about adjusting the height.

Need to get more comfortable with flexbox before integrating it into your own sites? We recommend Flexbox Froggy - A game for learning CSS flexbox. This is a great way to get more hands-on experience before diving in on an actual site.

Subscribe to our newsletter!

A few times a year we send out a newsletter with tips and info related to Craft CMS, technical SEO, and things we find interesting. No spam. Unsubscribe any time.

You Might Also Like

Craft vs WordPress

The Ultimate Craft CMS vs. WordPress Guide

Ryan Masuga Jan 27, 2021  ·  12 minute read

Craft CMS and WordPress are often brought up in comparisons for projects of certain size and scope. You might be considering one or the other for your next web project, either for your own company or for a client project. This is our comparison, based around points that impact our own client projects.

Selectors hero

6 CSS Selectors That Will Save You Time and Markup

David Musk Nov 25, 2020  ·  6 minute read

Here are six lesser-known CSS selectors we've used to help our clients in the past year. These aren't fancy, experimental features that only work on the latest browsers. Instead, they're practical tools you can start using today.