tags:

views:

37

answers:

2

I am working on a layout for a new site, and I'm having some trouble achieving what I want with the CSS. First of all, I want everything to always stay within the view of the current browser window, with scroll being in my content and not the browser itself. I have an outermost DIV which acts as my "wrapper" for the site displayed centered, with a set width, and having 100% height of the bowser window. Inside of this I place a header and all of this works as intended in all interested browsers.

However, once I place my actual content DIV inside this "wrapper" I am unable to define it to be the size I want. If I simply give it margins or padding to make up for the header I have absolutely positioned, the content will overflow and I can't set scroll. And if I try to set the size directly, there are no values I can put in that will work since the margins/padding will add to the size and it will now be bigger than the current browser window, and overflow.

Are there any styles people can think of I can use on the wrapper/content DIV(s) to get the desired look? Here is a diagram illustrating the look I want.

A: 

The following assumes, that you have a fixed height for your header (not a percentage). This example uses px values to make it easier to inspect with Firebug, but it works the same with em.

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Title</title>

    <style type="text/css">

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

        #wrapper {
            position:relative;
            width: 400px;
            height: 100%;
            margin: 0 auto 0 auto;
        }
        #header {
            position:absolute;
            margin-top:0;
            height: 70px;
            width: 400px;
            background-color: #ccc;
        }
        #content {
            position: absolute;
            top: 70px;
            bottom: 0;
            width: 400px;
            overflow: auto;
            background-color: #99c;
        }
    </style>

</head>
<body>
    <div id="wrapper">
        <div id="header">Header</div>
        <div id="content">
            Content<br/>
            Content<br/>
            Content<br/>
            Content<br/>
            Content<br/>
            Content
        </div>
    </div>
</body>
</html>

The important point is, that #content doesn't use a height at all - it uses a combination of top and bottom instead.

Note: I'm pretty sure, that modifications will be required for IE 6 ...

Chris Lercher
Thanks! This actually looks the way I want in all but IE6 (which I'm not too worried about now that IE8 is gaining traction). I never knew you could specify both top and bottom for positioning. I had assumed one would override the other, instead of working together.
Wolfgang
Just a side-note: this requires a `<!DOCTYPE>` in order to render correctly in IE7+.
Wolfgang
A: 
<style>
html, body {
    margin: 0;
    padding: 0;
    text-align: center;
    overflow: hidden; // prevents scroll bars in browser window}
#content_wrap {
    width: 800px; // set this to whatever you want
    margin: 0 auto; // centers the div
    text-align: left; 
    height: 100%; 
    position: relative; // basis for absolute positioning}
#header {
    position: absolute; 
    top: 0; 
    left: 20px; 
    width: 760px;}
#content {
    position: absolute; 
    left: 0; 
    top: 20%; 
    height: 80%; 
    overflow: scroll; 
    overflow-x: hidden;}
</style>

The challenge with this approach, and any that I can think of, is that without CSS expressions, you can't make the #content div height extend to the bottom of the screen except as a percentage. The trouble with this is that your header will not have a fixed amount of room in which to work. There's no CSS representation for your ideal "#content div should have 200px margin on the top and extend to the bottom of the page" set of attributes.

You could do this easily with JS, however.

Steven Xu