How to keep the Footer at bottom using CSS Flexbox
Table of Contents
When you're working on an HTML page, especially the homepage, you might have a footer.
A footer is where you can place your most important links, copyright information, and other information that you want to be visible on every page.
Since footers go at the bottom, you need to make sure it is the last element on the page, no matter the size of the page.
In this post, we'll learn how you can use CSS flexbox to ensure that your footer remains at the bottom of the page.
HTML Markup
To start, let's create a basic page that uses a footer.
HTML<!DOCTYPE html>
<html>
<head>
<title>Example page</title>
</head>
<body>
<article>
<header>header</header>
<main>main</main>
<footer>footer</footer>
</article>
</body>
</html>
We just have a basic article element, with a header, main, and footer.
Try it out yourself:
- HTML
As you can see, without any styles, the footer just sits right below the main element, instead of being at the bottom of the page.
Now let's move on to the styles.
CSS Styles
With our HTML markup established, we can apply the following styles to get the footer to stay at the bottom, regardless of the amount of content on the page.
CSShtml,
body {
height: 100%;
}
article {
display: flex;
flex-direction: column;
min-height: 100%;
}
main {
flex-grow: 1;
}
Try it below:
- HTML
- CSS
That's pretty much it. Now the footer is at the bottom of the page even though there isn't enough content to fill the page.
When there is enough content, the footer will follow naturally.
Conclusion
In this post, we looked at how to use CSS flexbox to ensure that your footer is at the bottom of the page.
Using the HTML markup and styles above, you can have a footer that remains at the bottom of the page even when there is not enough content to push it down.
Hopefully this helped you and thanks for reading!
- Managing PHP Dependencies with Composer
- Getting Started with Svelte
- Getting Started with Electron
- Best Visual Studio Code Extensions for 2022
- How to deploy a Node app using Docker
- Using Puppeteer and Jest for End-to-End Testing
- How to Scrape the Web using Node.js and Puppeteer
- Getting Started with Moment.js
- Creating a Twitter bot with Node.js
- Getting Started with Vuex: Managing State in Vue
- How To Create a Modal Popup Box with CSS and JavaScript
- Getting Started with Moon.js