I see two solutions here.
Using display: flex;
You can make a column layout that covers whole screen (100vh
) made of two containers.
- Top container would manage the page content
- Bottom container would be the footer that would always be "sticky", although not literally CSS sticky.
Example:
.container {
height: 100vh;
background: blue;
display: flex;
flex-direction: column;
}
.content {
height: 100%;
overflow: auto;
}
footer {
height: 60px;
background: red;
}
body {
margin: 0;
padding: 0;
}
<div class="container">
<div class="content">
<div>Ping</div>
<div>Ping</div>
<div>Ping</div>
<div>Ping</div>
<div>Ping</div>
<div>Ping</div>
<div>Ping</div>
<div>Ping</div>
</div>
<footer>This is the footer</footer>
</div>
Using position: sticky;
Given two containers (main
and footer
), you need to apply a min-width: calc(100vh - footerHeight)
on the main container, while footer is sticked to the bottom of their container.
This means that the main
container will always cover at least 100vh - footerHeight
.
Example:
body {
padding: 0;
margin: 0;
}
footer {
background: red;
position: sticky;
bottom: 0;
height: 60px;
}
.content {
min-height: calc(100vh - 60px);
display: flex;
flex-direction: column;
position: relative;
}
<div class="content">
<p>Ping</p>
<p>Ping</p>
<p>Ping</p>
<p>Ping</p>
<p>Ping</p>
</div>
<footer>This is the footer</footer>