Click to See Complete Forum and Search --> : Keeping the footer at the bottom


Webskater
06-06-2007, 06:40 AM
Newish to css layouts. I have a page that has the common layout of a series of divs 800px wide in the middle of the page.

So I have a header div at the top, a contents div below that and a footer div below that.

Lots of the pages have very little content - can I force the footer to stay at the bottom of the screen - regardless of the screen size the user is using?

Thanks for any guidance.

timdenty
06-06-2007, 06:56 AM
There is a good article at alistapart.com which discusses this. Their website seems to be down temporarily though, but you can still preview some examples if you google 'alistapart footer'...

http://www.alistapart.com/d/footers/footer_css3.html

Webskater
06-06-2007, 08:17 AM
Thanks for that link. Site was working intermittently when I looked at it - I reckon IE 7.0 is to blame.

Webskater
06-12-2007, 04:31 AM
If I use this:

#footer {
position: absolute;
bottom: 0;
margin:0;
clear: both;
padding: 5px 0px 2px 0px;
height: 20px;
width: 800px;
background: #FFC820;
}

The footer appears at the bottom of the screen, which is where I want it ... unless, of course, the content in the div above it is longer than the screen - in which case I want the footer to appear at the bottom of the contents div.

How can I achieve this?

i.e. if the content in the 'content' div does not fill the screen - move the footer to the bottom - if the content does fill the screen, make the footer appear after the content.

Thanks for any help.

Centauri
06-12-2007, 07:07 PM
The trick to this layout is to have a reference to page height. A container set to a minimum of 100% of screen height can do this, but the container's parent and its parent (body and html) need to be locked to the 100% height as well. If the footer follows the main container, it can be given a negative top margin equal to its height, which "pulls" it back up over the main container. A bottom padding on the content div will ensure that content does not go behind the footer. As IE6 doesn't understand minimum height, it gets the 100% height (which it treats like minimum height) applied via a * html hack.

Consider this simple layout:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
<!--
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
}
#wrapper {
width: 800px;
margin: 0 auto;
min-height: 100%;
}
* html #wrapper {
height: 100%;
}
#header {
height: 80px;
}
#content {
padding: 10px 10px 37px;
}
#footer {
width: 800px;
height: 20px;
margin: -27px auto 0;
padding: 5px 0 2px;
background-color: #FFC820;
}
-->
</style>
</head>

<body>
<div id="wrapper">
<div id="header"><h1>header</h1></div>
<div id="content">Content Goes Here</div>
</div>
<div id="footer">Footer</div>
</body>
</html>


Cheers
Graeme

Webskater
06-13-2007, 02:30 AM
Hi Graeme - thanks very much for your help.

In my layout I have a background image in the body to create the effect of a centered 800 px wide layout - white in the middle with a graduated colour type background either side.

I have had a play with your solution and it does exactly what I want in terms of pinning the footer to the bottom of the screen if the contents of the content div is not enough to reach the bottom of the screen and moving down with the content div if the contents are bigger than the screen.

But, if the content in the content div is not enough to reach the bottom of the screen then the background is showing through - below the content div down to the footer. Can I force the content div to occupy the full height between the header and footer in all circumstances?

Thanks again.

By the way, what is the significance of the asterisks in your code? I have never seen this before.

Webskater
06-13-2007, 02:32 AM
Deleted - posted twice

Centauri
06-13-2007, 03:21 AM
Unfortunately you can't force the content to occupy the available height. If you have that repeating background, then the centre section of that could be used to provide the content background, or another repeating background could be used on the #wrapper div - either of these make it look like the content goes all the way.

The asterisk is a universal selector - in the first use it is used alone to zero the browser default margins and paddings of all elements on the page.

The second use involves an IE6 hack - IE6 seems to think that there is another "wrapper" around the html element, and the "* html #wrapper" means the #wrapper div enclosed by the html element which is enclosed by any other element - a condition that only IE6 and below thinks is valid.

Cheers
Graeme