This blog has moved!

Please follow the twenty08 blog on tumblr! We'll have new updates on MobileChat, QuickShareIt, and other products there!

http://tumblr.twenty08.com

HowTo: Align a footer to the bottom of the page in XHTML Strict across all three browsers!


This is a bit tricky to get to work across all three major browsers (internet explorer, firefox, and safari) while following XHTML Strict (this will still work in HTML4 strict as well.).

Most people can get the footer to go to the bottom of the window, however when the content forces the page to scroll but the footer still stays at the bottom of the window, and not the bottom of the page.

You can see the working example here:
http://www.twenty08.com/labs/bottomalign/index.html

You can continue reading, or just check out the source and go from there.

For starters, you need three elements: A wrapper element, a content element, and a footer element. For purposes of this HowTo, the wrapper element will be called “parent” the content element will be called “content” (clever huh?) and the footer element will be called “footer”.

The basic structure should be as follows:

[...]
<body>
<div class="parent">
<div class="content"> </div>
<div class="footer"> </div>
</div>
</body>
[...]

The rest of this is done via CSS. You’re going to need a main stylesheet file, we’ll call ours style.css and then an IE stylesheet file, which we’ll call ie.css.

Everything below takes place in the main.css stylesheet, until I say otherwise:

You need to define your body height as 100% height otherwise you’re going to run into a few issues, so:

body {
margin: 0px;
height: 100%;
font: 12px "Lucida Grande", Lucida, Verdana, sans-serif;
}

Next you need to set up the parent class. The min-height is for Safari and Firefox to force the div height to 100%, while still expanding. We’ll address IE later. Next you want to position this absolutely on the page at 0,0 and go the full page. You’re essentially creating a clone of the body element right now. Here is some sample code below:

.parent {
display: block;
min-height: 100%;
width: 100%;
position: absolute;
top: 0px;
left: 0px;
z-index: 0;
}

Next you’re going to setup the content class, this is the easiest element you’re going to setup. You want to define the bottom padding of the element with at least the same height as the footer is going to be, this prevents the footer from laying on top of your text.

.content {
padding-bottom: 50px;
}

Lastly we need to define the footer class. You need to absolutely position this to the bottom of the parent element, so for this you’ll do bottom: 0px and left: 0px. You need to set the height of the footer to whatever you need it to be, remember, this number should not be greater than the number you defined for the content bottom padding or the footer will overlay your text. The last required thing is to set the z-index to some high number so nothing from content gets above it.


.footer {
position: absolute;
left: 0px;
bottom: 0px;
width: 100%;
display: block;
height: 45px;
z-index: 999;
padding: 0px;
margin: 0px;
}

This is pretty much it as far as style.css goes. The only thing you need to do in ie.css is the following:

.parent {
height: 100%;
}

IE doesn’t support min-height like Safari and FireFox do, but using height inplace of min-height in IE, has similar results.

You’re going to want to include the two files in the section of your website as shown below:

<style type="text/css" media="all">
@import "./style.css";
</style>
<!--[if lte IE 6]>
<style type="text/css" media="all">
@import "./ie.css";
</style>
<![endif]-->

This will import style.css on all browsers, and only import the ie.css file on IE browsers.

Thats pretty much it, see the example below to for a full reference on how this is done:
Align footer to bottom of page in XHTML Strict with 100% Height

Hope this helps anyone who’s having trouble doing it!

Update: Forgot to include IE7 support. Only change neccessary is changing the conditional comment statement from “if IE” to “if lte IE6″ and it will work fine.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.



Other Posts
FaceBook Developers Having Fun?
New QuickShareIt Web Site Launched!

Reader Comments

I’m having troubles getting this to work in IE7, did you not apply the IE RULE/HACK to your example?

Check the update, sorry about that!

works like a charm now, thanks!

I was so lucky when I found the code, because I’m having problem with putting the footer at the bottom.

But problem still exist, have a look at http://tonysbilvard.com/beta/scratch.html

Which works - and then…
http://tonysbilvard.com/beta/scratch2.html

With more content and does not work… please help me out here guys.

Can you help with setting this up on a centered page?

What I’m doing is adding to the .content class:

margin: 0 auto;
width: 800px;

Works like a charmed in FF1.5, FF2, IE7 (not tested in safari) but in IE6 the .content class is aligned to the left.

Ideas anyone?

Don’t worry, figured it out! For centered page version of this code that works in IE7, FF1.5, FF2, and *IE6*, add this to the code:

body {
text-align:center;
}

.content {
text-align:left;
margin:0 auto;
width:800px;
}

:)

How to solve this problem if you have more content? Because position:absolute;
Any ideas???

sorry, found solution :)

How to solve this problem if you have more content? Because position:absolute;
Any ideas???

She just posted this, then said she found the solution. I still need the solution to the absolute positioned element problem. Thanks!

Great code - BUT save making a seperate IE.CSS how about adding this to .parent:

min-height:100%;
height:100% !important;
height:100%;

That works in IE and FF now without an extra CSS page.

emerald rings and gold rings www.emeraldring.fora.pl

gmc trucks here www.gmctruck.fora.pl
gmc from america www.gmctruck.fora.pl
real gmc www.gmctruck.fora.pl

and www.emeraldring.fora.pl rings

The following tests were done strictly with the link provided by the original post.
1st Mac
Ok, the original example listed on this page does NOT work in Mac Safari 1.3.2 or Mac Explorer 5.2.3. The footer does not ’stick’ to the bottom.
It does work in Mac Fire Fox 2.0.0.6, and Netscape 7.1 perfectly

Now PC
It works fine on PC in IE7.0.5730.11, Fire Fox 1.5.0.8, and Netscape Navigator 9.0b3

Result: I would say this code is 100% PC approved and too buggy for Mac. Which I regret to say that to me is unexceptable. I’m still seeking this css setup that will work in all browsers. (with the exception of IE on Mac - Anyone actually still use this as their default browser on Mac? Its depricated/discontinued)

Note: Here is another close but seemingly not perfect attempt.
http://www.alistapart.com/articles/footers/

Lastly: To all who think they have the footer aligned bottom css that is all browser compatible. please show the example in a ‘CENTERED’ design. Left align is so 1999… or Old School. Just look at all of today’s biggest sites. yahoo, youtube, facebook, google, myspace, msn, wikipedia. All centered sites.
width: 800px; is common.

Sorry I don’t mean to sound snotty but this is a tiring search with many near misses.

Testing

Type your comment here.

somestrangetextvista

Thank you very much!

Thank you, looking forward to many more visits!

Thank you, looking forward to many more visits!

Thank you for the informative work!

You might find this link more helpful for fixed width centered columns:

http://www.menukit.com/tut1ColDivs.htm

Why make another css file for IE? Just put _height:100%; in .parent. Because of the _ in front of it Firefox will ignore it, but IE will still use it. That way you don’t have to use two css files.

Having trouble in Firefox where the footer overlaps just some of the content when window size is changed