How to make a sticky footer using CSS?
A couple modern methods using a sprinkle of flex box CSS and the following HTML structure:
<body>
<header></header>
<main></main>
<footer></footer>
</body>
Method 1: (fixed height footer) Apply display:flex
and flex-direction:column
to the body
. Apply flex:1
(flex-grow:1
) to the main
element.
The main element will grow vertically to occupy any empty space, thus making the footer stick to the bottom.
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin:0;
}
header {
background-color: #cffac7;
height:50px;
}
main {
background-color: #f8e2ff;
flex:1;
}
footer {
background-color: #fceec7;
height:50px;
}
<header></header>
<main></main>
<footer></footer>
Method 2: (fixed height footer) Apply display:flex
and flex-direction:column
to the body
. Apply margin-top:auto
the footer
.
You're done, because auto margins inside flex containers absorb all available free space, making the footer stick to the bottom. Note that this is not dependent on main having any height or content. In this case, we've given main no flex rules at all, and so it gets the default value of flex:initial
(flex: 0 1 auto
).
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin:0;
}
header {
background-color: #cffac7;
height:50px;
}
main {
background-color: #f8e2ff;
}
footer {
background-color: #fceec7;
height:50px;
margin-top:auto;
}
<header></header>
<main></main>
<footer></footer>
Method 3: (fluid height footer) This is really the same technique as in #1 but with elements that have no intrinsic height. By virtue of the ratio between the (unitless) flex-grow
values given to the competing elements, main
will grow at five times the rate as the header
and footer
.
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin:0;
}
header {
background-color: #cffac7;
flex:1;
}
main {
background-color: #f8e2ff;
flex:5;
}
footer {
background-color: #fceec7;
flex:1
}
<header></header>
<main></main>
<footer></footer>
Following a clean method implemented from an online source no longer available (dead link), the minimum code you should need for your page would be (note - probably best to use #bottom-footer
instead of footer #bottom-footer
for the selection of your footer - that could be part of the issue):
html {
position: relative;
min-height: 100%;
}
body {
margin: 0 0 100px; /* bottom = footer height */
}
#bottom-footer {
position: absolute;
left: 0;
bottom: 0;
height: 100px;
width: 100%;
}
This works perfect. It's the example from W3SCHOOLS
https://www.w3schools.com/howto/howto_css_fixed_footer.asp
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: red;
color: white;
text-align: center;
}
</style>
</head>
<body>
<h2>Fixed/Sticky Footer Example</h2>
<p>The footer is placed at the bottom of the page.</p>
<div class="footer">
<p>Footer</p>
</div>
</body>
</html>