In order to get CSS percentage height to work, the element's parent must have a height defined. As a result we can do it with the following markup:
<body>
<div id="content">
<div id="header">Header</div>
<div id="text">
Text content of your page
</div>
</div>
<div id="footer">Footer</div>
</body>
All of the site's content except for the footer is contained in the #content
element. This then uses a min-height: 100%
declaration to be always at least 100% of its parent's height. Since its parent is the body and has a height declaration, this works as expected.
Finally the footer is brought into view with a negative margin and padding-bottom is added to the #text
element so its content doesn't get accidentally overlapped.
The CSS is as follows:
html, body {
height: 100%;
padding: 0;
margin: 0;
}
#content {
min-height: 100%;
/* IE6 will need height: 100% defined in an IE6 only stylesheet */
}
#header, #footer {
height: 100px;
}
/* Bring the footer into view */
#footer {
margin-top: -100px; /* footer's height */
}
/* Make sure footer doesn't overlap #text element */
#text {
padding-bottom: 100px; /* footer's height */
}
You can see it in action here. If you add more filler text you'll see the footer is properly pushed down.