tags:

views:

268

answers:

3

I want to have a div that grows when you add more content in it, has at least the height of the viewport and has a header and a footer sticking to the top and bottom. I came up with the following which works fine in IE7 but doesn't work in ff3.5.

This is the HTML (add repeated 'Lots of text
' for main_body to grow out of the viewport):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Testing 123</title> 
<link rel="stylesheet" href="css/testing.css">
</head>
<body>
<div id="main_body">
    <div id="header"></div>    
    <div id="content">
    Lots of text<br>
    </div>
    <div id="footer"></div>    
</div>
</body>
<html>

This is the css:

* {
    margin: 0px 0px 0px 0px;
    padding: 0px 0px 0px 0px;
    border: none;
    z-index: 10;
    font-family: Arial;
    font-size: 20px;
    text-decoration: none;
    text-align: left;
    }
html, body {
    height: 100%;
    background-color: rgb(255, 255, 255);
    }
#main_body {
    position: relative;
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0px 20px 0px 20px;
    }
#header {
    position: absolute;
    top: 20px;
    left: 0px;
    height: 50px;
    width: 100%;
    background-color: rgb(40, 40, 40);
    }
#content {
    margin: 80px 10px 50px 10px;
    }    
#footer {
    position: absolute;
    bottom: 20px;
    left: 0px;
    height: 20px;
    width: 100%;
    background-color: rgb(40, 40, 40);
    }

I think this should work according to specs. And it does in IE but not in ff3.5. Pleae help.

EDIT: I found out (thanks to Jeepstone) that it works fine when I change margin to padding in #content.

A: 

It works for me in FF 3.5.7. You can try to set the positioning from relative to static as well, which might solve it. Let me know!

Eggie
+1  A: 

In fact it doesn't work for me in IE8, FF3.5 and Webkit browsers and Opera.

I can't recall the actual reason but if you add a something e.g. &nbsp; after the "main_body" div, it works.

<div id="main_body">&nbsp;
o.k.w
Thanks, it works partly when I follow your suggestion. When the page is longer than the viewport, the footer doesn't stick to the bottom of the page.
jasperdejong
It'll need a different solution for that. Have you checked out the references from Jeepstone?
o.k.w
+1  A: 

100% height is not straight forward. You need to do something like http://www.xs4all.nl/~peterned/examples/csslayout1.html.

Incidentally, where you are resetting *, you should look at Eric Meyers CSS Reset http://meyerweb.com/eric/tools/css/reset/ as resetting everything can cause problems.

Jeepstone
Thanks for your links Jeepstone! The first one solved my problems.
jasperdejong