Adding a sticky footer using Flexbox
Hi everyone,
I want the footer element to reside permanently on the bottom of the page. Using Flexbox, this should be easy to accomplish, but I have not had any success. I feel embarrassed to ask this question.
There is a flex container with three child elements; the parent element (flex-container) is set to display: flex.
<html>
<head>
<meta charset="UTF-8">
<title>HTML</title>
<style>
*, *::before, *::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
min-height: 100dvb;
}
.flex-container {
display: flex;
flex-direction: row;
}
.flex-container > div {
height: 150px;
width: 200px;
color: #FFF;
}
.header {
background-color: peachpuff;
}
.main {
background-color: crimson;
}
.footer {
margin-left: auto;
background-color: darksalmon;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="header"></div>
<div class="main"></div>
<div class="footer"></div>
</div>
</body>
</html>
When I set the flex-direction to row and the footer element to margin-left: auto as it appears above, everything works fine. The footer element is pushed to the far right side of the viewport like this.
But when I set the flex-direction to column and the footer element to margin-top: auto, the elements stack; there is no gap between the main and footer elements. I want the footer to be pushed to the bottom.
What am I doing wrong? Is there a better way?
Thanks.
Mark
