Keeping the Footer at the Bottom
Published: 2020-02-23 21:18:07

*[If you've found this article useful, consider donating.](https://www.patreon.com/alanjc)*

<i><a href="https://blog.alanjc.com/articles/keep-the-footer-at-the-bottom.html">This article was originally published on my blog.</a></i>

So recently I had to use the <code>&lt;footer&gt;</code> tag for a website I was building. To be honest, in all of my web development projects that I've made so far, I've never had the need to use the footer. That was until last week.

What's the big deal? It's just a footer tag that you include before closing out the body tag right? Sure, but the problem with footers is that by default, they don't stay at the bottom. Let me explain.

If your body tag were to contain the following:

{% gist https://gist.github.com/AlanConstantino/052d00ade983f5a4a78992386c2b99c3 %}

your footer would be pushed all the way to the top. This happens because there isn't enough content to fill up the page and push the footer down.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/f5wkb7i52latej3v30f9.png)

For the most part, if your website has enough content to fill up the page, your footer should be pushed down to the bottom. However, on bigger screens, this might still be an issue.

So, how do we keep that pesky footer at the bottom? Well, today I'll be showcasing three solutions to this problem. The first and simplest solution is using flexbox, the second solution is using grid, and the final solution is using a wrapper. Alright, let's get started.

<h3>Flex Solution</h3>

First, the simplest, and in my opinion, the best solution, using flexbox. I'd say this is the simplest because it isn't that much code to write and it's simple to understand.

This is what our HTML looks like:

{% gist https://gist.github.com/AlanConstantino/5a3643a2e3a8f95eb81de697beb2d5d8 %}

The HTML is going to look pretty much the same for all of these solutions, I'll be sure to point out the differences along the way.

Very briefly, in our <code>&lt;body&gt;</code>, we have a <code>&lt;header&gt;</code>, a <code>&lt;main&gt;</code>, and a <code>&lt;footer&gt;</code>. Inside our <code>&lt;main&gt;</code>, we have a <code>&lt;p&gt;</code> that just holds some lorem ipsum text.

This is what our CSS looks like:

{% gist https://gist.github.com/AlanConstantino/ff639f4799cf40ddffae00cffccdae1e %}

Again very briefly, I remove all the padding and margin for everything. Our body is displayed as flex, has a 'flex-direction' of column, its minimum height is 100vh or 100 viewport height, and its background color is blue. As for the header, I just colored it green. Finally, I gave the footer a background color of red and I set its top margin to auto. This is important because, without this, the footer wouldn't stay at the bottom.

Now our footer looks something like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4xwx8ib7uhs80balg184.png)

Let me explain how this works. We set our <code>&lt;body&gt;</code> to be displayed as a flex column and set its minimum height to be the full size of the viewport height. You can think of the <code>&lt;body&gt;</code> as a container that expands for the full height of the page. Now by setting our <code>&lt;footer&gt;</code>'s top margin to auto, we're essentially telling it to stay at the bottom of the <code>&lt;body&gt;</code> container. And that's how you use CSS flex to keep the footer at the bottom.

<h3>Grid Solution</h3>

Let's move on to the second solution, using CSS grid. Now I don't know about you but I don't like grid. I'd much rather use flexbox, but, as much as I hate to admit it, sometimes there is just no avoiding it. 

The HTML code will be exactly the same as for the flex solution, the only thing that will differ will be the CSS link tag.

{% gist https://gist.github.com/AlanConstantino/6658ce7e99b53f0a16ee70de792bcdc5 %}

The CSS for the grid solution looks something like this:

{% gist https://gist.github.com/AlanConstantino/9568524fee37bff184a54848557d32ad %}

Just like before, we first remove all the padding and margin for everything. Then we make the html height 100% and I made all the text color white. Next, our body has a minimum height of 100% and is displayed as a grid with 3 rows and has a background color of blue. Those rows will be the header, main, and the footer. The header has a background color of green and will start and end in the first row of the three that we defined for the grid. Finally, our footer will have a background color of red and will start and end at the third row of our grid.

Now let me explain how this works. In the body, we define three rows, the first and last are of size 'auto' while the row in the middle is a set size of 1fr. We use 'auto' for the first and last rows so they only take up the space they need.

We then set our header to be the first row of the grid keeping it at the top while setting our footer to the last row in our grid keeping it at the bottom. That's it!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e80ncbl8oewy8986lt1p.png)

You could manually define main to be in the second row like the following:


<pre>
<code>
main {<br>
&emsp;&emsp;grid-row-start: 2;<br>
&emsp;&emsp;grid-row-end: 2;<br>
}
</code>
</pre>


but I just didn't do that.

<h3>Wrapper Solution</h3>

Finally, we have reached our last solution which is using a wrapper. The HTML for this solution is as follows:

{% gist https://gist.github.com/AlanConstantino/47d89e7f5baaf8c9cd841c9594d6392f %}

The only difference here is that we use a <code>&lt;div&gt;</code> as a wrapper to wrap our <code>&lt;header&gt;</code> and our <code>&lt;main&gt;</code> tags. We also give the <code>&lt;div&gt;</code> a class of "wrapper."

The CSS for the wrapper solution is the following:

{% gist https://gist.github.com/AlanConstantino/a5764b31f026011782aca3ed6f8a57ec %}

Just like before, we first remove all the padding and margin from everything. Then we assign background colors to the header, main, and footer. The header is green, the main is blue, and the footer is red. Now, we must define both the html and the body as a height of 100%. Next, we make a class called "wrapper" and give it a minimum height of 100%. Now, this is where things get a little... gross.

In main, we give it a bottom padding of 30px, this will be the size of the footer. Now in our footer, we give it a height of 30px and a top margin of -30px.

Let me explain why it needs to be done this way. So our main has a bottom padding of 30px. This is so that when there is enough content on the screen, it doesn't overflow into our footer like so:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/f60541jd4mp1x2ff636d.png)

The height of the footer should also be the same height as the bottom padding for our main, 30px. This should also be applied to the top margin but as a negative, if not then you end up with something like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y3wxc5jvqnqpcl9wehzu.png)

You can see that there is a space towards the bottom of the screen in between our <code>&lt;main&gt;</code> and our <code>&lt;footer&gt;</code> and that's definitely not what we want. That's why we apply the top margin as a negative while making sure it's the same size as the <code>&lt;footer&gt;</code> height and the bottom padding of <code>&lt;main&gt;</code>. Now the footer should be displayed properly:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jbr74f8n9rhea2o6y1g8.png)

There we go, much better.

<h3>Conclusion</h3>

In conclusion, there are many ways to keep the footer at the bottom, however, in this article, I just mentioned three, the flex way, the grid way, and finally the wrapper way.

All of these solutions are great but the best one, in my opinion, is the flex one. It's easy to implement and you don't have to deal with a grid or a wrapper. The grid solution may be best for when you can't use flexbox or you’re stuck using a grid, while the wrapper solution may be best for when you already know how big the size of the footer is going to be.

You should keep these solutions in mind when thinking about how you're going to keep that pesky footer at the bottom of the page. You should choose the solution that best fits your needs.

You can check out the full code <a href="https://github.com/AlanConstantino/web-cheat-sheet/tree/master/examples/keep-footer-at-the-bottom">here</a>.

Thanks for reading and have a good one!