tags:

views:

397

answers:

2

I am designing a website layout in which all of the content is held in a central column of a fixed width. I want this central column to have a different background color than the rest of the page. I also want the central column to extend from the very top of the browser to the very bottom.

I am able to successfully do this using a background image of dimensions 1000x1, as follows:

 html
{
    background: #333333 url(./images/global/background.png) repeat-y center;
}

body
{
    margin: auto;
    width: 1000px;
}

This works great in most browsers, but I would really prefer to be able to do it without an image.

I have tried setting "html" and "body" to have a "height: 100%" and then giving "body" a background color, but if there is enough content to warrant scrolling, the background only has a height equal to that of the browser and when you scroll down, the background stays behind.

Any tips are appreciated.

A: 
body { 
    text-align: center; 
    margin: 5em 0 0 0; 
    vertical-align: middle; 
}

#content { 
    width: 760px; 
    text-align: left; 
    margin: 0 auto; 
}

Details here.

Dave Swersky
This isn't really what Paul is asking. He wants the column to cover the entire height of the page. This just covers the width and centering part of it which he already has.
Welbog
+3  A: 

The solution is to use a wrapper div that has 100% height and a separate content div that will extend if the content inside is long enough both having the background color set. Here is an example (tested in Firefox, Chrome and IE7):

<html>
    <head>
        <style type="text/css">
            html {height: 100%}
            body {
                height: 100%; 
                margin: 0; 
                text-align: center;//for IE7
            }
            div#wrapper { 
                background-color: #efefef;
                width: 720px;
                margin: 0 auto;
                height: 100%;
                text-align: left;
            }
            div#content {
                background-color: #efefef;
            }
        </style>
    </head>
    <body>
        <div id="wrapper">
            <div id="content">
                <div style="height: 2000px">test</div>
            </div>
        </div>
    </body>
</html>
Aleris
That's close to what I want, however the color (#efefef) doesn't quite extend to the top of the window, and when scrolled to the bottom, doesn't quite touch the bottom of the window either in some cases.
Paul Williams
Does for me, under what condition does the div not extend to the full height of the viewport? Also: If you use a correct doctype to force IE in to standards mode, you don't need the text-align:center; text-align:left; malarky.
garrow
make sure the body AND html have no padding/margins aswell
sanchothefat
I did some fiddling in my markup and figured out what my problem was. The last <p> element on my page had a margin on the bottom.It works great now, thanks.
Paul Williams