On a custom HTML page that I created, I decided to include a footer using Bootstrap. In this example, it demonstrates how the footer can be positioned at the bottom of the page without being placed inside a container element (directly within the body element, below a container).
Initially, when implementing this on my own page, the footer appeared correctly at the bottom. However, upon navigating to longer content pages, the footer started shifting to the bottom of the browser instead of staying within the body element. This resulted in the container element appearing behind the footer and underneath it, rather than having the footer aligned with the bottom of the body element.
Take a look at the following code snippet:
.body {
overflow: scroll;
background-color: tomato;
/* The height is specifically set for creating an overflow scenario,
replicating my issue. When the body content exceeds this height,
the browser displays a scrollbar, but the footer does not remain at
the bottom as intended. It may shift to the middle or top,
depending on the length of the body content. */
height: 300px;
position: relative;
}
.container {
background-color: yellow;
/* This value is dynamic; removing it could help you see
the impact on the footer alignment. Feel free to adjust this value
to better understand the issue. */
height: 500px;
}
footer {
background: #e0f2f7;
position: absolute;
bottom: 0;
width: 100%;
}
<div class="body">
<h2>
Foo
</h2>
<div class="container">
Lorem ipsum dolor sit amet
</div>
<footer>Lorem ipsum</footer>
</div>
Is there a way I can ensure that my footer behaves like the one in the Bootstrap example (remaining fixed at the bottom) without needing to nest it in a specific container element?