To ensure the footer is positioned correctly, you can utilize the properties height
and min-height
. This will allow the footer to be automatically pushed after reaching the height of the div. If the content overflows, the min-height
will expand accordingly.
Although implementing the functionality with JavaScript would be more organized, this example demonstrates achieving the desired outcome using CSS alone.
Below is an illustration with non-overflowing content:
* {
margin: 0;
padding: 0;
}
div {
min-height: 80vh;
}
footer {
color: white;
background-color: black;
height: 20vh;
display: flex;
align-items: center;
justify-content: center;
}
<body>
<div>
Some Content
</div>
<footer>
Random Footer
</footer>
</body>
Now, let's consider a scenario with overflowing content:
* {
margin: 0;
padding: 0;
}
div {
min-height: 80vh;
}
footer {
color: white;
background-color: black;
height: 20vh;
display: flex;
align-items: center;
justify-content: center;
}
<body>
<div>
<p>Random Content 1</p>
<br />
<p>Random Content 2</p>
<br />
<p>Random Content 3</p>
<br />
<p>Random Content 4</p>
<br />
<p>Random Content 5</p>
<br />
<p>Random Content 6</p>
<br />
<p>Random Content 7</p>
<br />
<p>Random Content 8</p>
<br />
<p>Random Content 9</p>
<br />
<p>Random Content 10</p>
<br />
<p>Random Content 11</p>
<br />
<p>Random Content 12</p>
<br />
<p>Random Content 13</p>
<br />
</div>
<footer>
Random Footer
</footer>
</body>