Forum Moderators: not2easy

Message Too Old, No Replies

A Floating Footer That Stays at the Bottom of the Page

Unless the page content is longer than the window.

         

Lance

11:45 am on Aug 18, 2004 (gmt 0)

10+ Year Member



I've been a member here for just a few weeks and already I see that a Floating Footer is one of the more frequently asked-for items. IIRC, it may even be what brought me here in the first place.

After looking at many fine examples of footer code from many different authors and playing around with it a little on my own, I have come up with what I consider to be a pretty versatile solution. It may not be the lightest possible way to do it, but it offers options for use in different page layouts with a little imagination.

I post this here so maybe those searching for somthing like this will be able to find this example easily, and so others can fine-tune my code even better. So, without further delay:

A Footer That Will Stay at the Bottom of the Page
(Works in IE6 quirks and compliant modes, and NN7.1)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Footer Demo</title>
<style type="text/css">
html, body
{
HEIGHT: 100%;
WIDTH: 100%;
MARGIN: 0px;
PADDING: 0px;
FONT-FAMILY: Arial, sans-serif;
}
#Contents
{
POSITION: absolute;
TOP: 0;
LEFT: 0;
MIN-HEIGHT: 100%;
WIDTH: 100%; /* Optional - Depending on your layout */
}
* html body #Contents
{
HEIGHT: 100%; /* Only IE sees this */
}
#Main
{
MARGIN-BOTTOM: 3.5em; /* Set this to the height of the footer */
BACKGROUND-COLOR: red;
COLOR: #fff;
PADDING: 0px;
}
#Footer
{
BACKGROUND-COLOR: #00f;
COLOR: #fff;
POSITION: absolute;
BOTTOM: 0px;
WIDTH: 100%;
TEXT-ALIGN: center;
}
</style>
</head>

<body>
<div id="Contents">
<div id="Main">
<!-- Begin Arbitrary Content -->
<div style="HEIGHT: 12em; TEXT-ALIGN: center;">
<br /><br />
I am the content that lives in the Main Section.<br />
I am the content that lives in the Main Section.<br />
I am the content that lives in the Main Section.<br />
I am the content that lives in the Main Section.<br />
I am the content that lives in the Main Section.<br />
I am the content that lives in the Main Section.<br />
</div>
<!-- End Arbitrary Content -->
</div>
<div id="Footer">
<div>I am the content that lives in the Footer Section.</div>
<div>I am the content that lives in the Footer Section.</div>
<div>I am the content that lives in the Footer Section.</div>
</div>
</div>
</body>
</html>

Old_Honky

2:19 pm on Aug 18, 2004 (gmt 0)

10+ Year Member Top Contributors Of The Month



If you want the footer to stay below the content even if the content is longer than the window, why not use the natural flow of the page and relative positioning like this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Title of the Page</title>
<style type="text/css">

body, html {
margin: 0;
padding:0;
border: 0;
width:100%;
height:100%;
}

.container{
position:relative;
height:100%;
width:100%; }

.footer{
position: relative;
width:100%;
text-align-center;
float: left;
margin: 0;
border: 0;
padding:0;
}

</head>
<body>
<div class="container">
Everything else but the footer goes here
</div>
<div class="footer">
Footer content
</div>
</body>
</html>

Lance

3:21 pm on Aug 18, 2004 (gmt 0)

10+ Year Member



Once I added the missing </style> tag, I tried it in both IE and NN. The footer content is always below the viewport.

Old_Honky

11:47 pm on Aug 18, 2004 (gmt 0)

10+ Year Member Top Contributors Of The Month



Well spotted, that was sloppy cutting and pasting on my part.

I've used that method myself and it works OK, perhaps I've over simplified and missed something out, I'll take a look at the code and repost tomorrow, unless of course one of the real experts corrects me first.

Old_Honky

12:16 am on Aug 19, 2004 (gmt 0)

10+ Year Member Top Contributors Of The Month



I've just checked it, I got a bit confused because in the site I took the code from I'm using an extra style sheet for IE (my footer is {position:fixed} for other browsers).
Try adding {top: -20px;} to the footer code (where the footer is 20px high)- you could also use negative percentages or ems

Lance

3:07 pm on Aug 19, 2004 (gmt 0)

10+ Year Member



It seems in NN, the footer won't drop below the viewport when the content exceeds the length either. I expect it wouldn't too much modification to make this work right in both IE and NN. Of course, then it starts to look familiar. ;)

Old_Honky

11:19 pm on Aug 19, 2004 (gmt 0)

10+ Year Member Top Contributors Of The Month



I don't understand I just made up a test page and it works perfectly in firefox 0.91, opera 7.5, IE6.0.28 and NN7.1. (Of course it looks like s**t in NN4.6 but that's such a small part of the "market" that it doesn't matter much).

What version of NN are you using?

This is the exact code I used, try a cut and paste and see what you get.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<title>Test Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css">
body, html{
margin: 0;
padding:0;
border: 0;
width:100%;
height:100%;
}

.container{
position:relative;
background-color: red;
font: normal 1.75em/2em Verdana, Helvetica, sans-serif;
color: #FFF;
height:100%;
width:100%; }

.footer{
position: relative;
top: -20px;
background-color: green;
font: normal 1em/1.25em Verdana, Helvetica, sans-serif;
color: #FFF;
width:100%;
text-align-center;
float: left;
margin: 0;
border: 0;
padding:0;
}
</style>
</head>

<body>
<div class="container">
Everything else but the footer goes here
</div>
<div class="footer">
Footer content Footer content Footer content Footer content
</div>
</body>
</html>

Lance

11:59 pm on Aug 19, 2004 (gmt 0)

10+ Year Member



Ah, okay. I see what you're seeing.

Replace your content <div> with this so you have longer content:


<div class="container">
Everything else but the footer goes here<br />
Everything else but the footer goes here<br />
Everything else but the footer goes here<br />
Everything else but the footer goes here<br />
</div>

With just one line of content you're not seeing the footer collapse, you're seeing the browser collapse.

And for what it's worth, my original posting wasn't asking a question. It was offering a solution. I understand my solution isn't the only solution, and likely isn't the best solution. But it is a good solution none the less.

Old_Honky

12:05 am on Aug 20, 2004 (gmt 0)

10+ Year Member Top Contributors Of The Month



Sorry Lance, you are right, and I am wrong. That code only works for 100% screen size (when you load up the container div to make it scroll vertically the footer doesn't drop to the bottom).

If you want the footer to be tagged on the bottom of the content whatever size then you can remove the {height:100%;} from the container div and the {top:-20px;} from the footer.

That will give you a page as big as your content, and if you have only a small amount of content on some pages your footer will be too high. You can adjust the page height by adding a spacer div inside the container div.
In the CSS style section


.spacer{
clear: both;
padding: 20px 0;
}

In the html


<div class="spacer">&nbsp;</div>

either adjust the padding in the css or add multiple spacer divs untill the page fits in the window.
That should give you the best of both worlds.

Old_Honky

12:26 am on Aug 20, 2004 (gmt 0)

10+ Year Member Top Contributors Of The Month



BTW I wasn't intending to criticise your solution, just offering another way.
I tried your code from your original posting and it works fine in IE, but in the other browsers the content won't expand to meet the footer no matter how much I load into the main section.