If you're looking to keep your footer fixed at the bottom of the page using just CSS, this solution should work perfectly for you.
Check out the implementation here: http://jsfiddle.net/zVLrb/
This method takes advantage of the behavior of elements with a position:fixed
. Essentially, it ensures that the footer stays at the bottom of the viewport on shorter pages without causing any interference with the content above it.
The key is setting a higher z-index for the page content than the footer. This creates an illusion where the footer remains fixed at the bottom of the window while the rest of the page scrolls over it. By adding a bottom margin equal to the height of the footer, we reserve space at the bottom for the footer to appear when needed.
This technique is compatible with all modern browsers. However, it's always a good idea to test in older versions like IE7 to ensure compatibility.
CSS Code:
.rest {
position: relative;
z-index: 100;
background: #fff;
margin-bottom: 200px;
overflow: hidden;
}
.footer {
height: 200px;
width: 100%;
background: #000;
position: fixed;
bottom: 0;
z-index: -1;
color: white;
text-align: center;
font-size: 20pt;
}
.footer p {
margin-top: 50px;
}
HTML Markup:
<div class="rest">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut feugiat
euismod urna, eget interdum eros elementum in. Morbi dictum molestie
porta. Morbi eget consectetur nibh. Etiam sed arcu ac elit dignissim
consequat.
</p>
<!-- Your content here //-->
</p>
</div>
<div class="footer">
<p>This is the footer</p>
</div>
Additional Note:
In some older versions of Internet Explorer, using a negative z-index might hide the footer completely below the body
layer. To avoid this issue, consider adjusting the z-index values to 2
for .rest and 1
for the footer. Test this change on different browsers to ensure optimal visibility of the footer.