开发者

another css 100% height question

Sorry but I can't get this to work. Should be a quick answer.

My html is laid out like so:

<html>
    <header> 
    ... 
    </header>

    <body> 
        <div class = "background"></div>
        <div class = "content">
        ...
        </div>
    <body>
</html>

The I want the background div to simply place a 1000px bac开发者_如何转开发kground colour down the entire length of the page. The content is then padded 40px on each side, inside this background colour.

The css is like so:

body {
    width:1000px;
    margin-left:auto;
    margin-right:auto;
}

.background {
    position:absolute;
    top:0px;
    width:1000px;
    height:100%;
}

.content {
    min-height:100%;
    padding-left:40px;
    padding-right:40px;
}

I thought it worked like so... The body div would expand to hold the min-height of the .content div. This means that 100% height of the .background div would fill the entire body and so the length of the page. However it does not. It only fills the window height. Where am I going wrong?

Thanks


As topek guessed, this will do it:

html, body{
  height:100%
}

The reason this works is because percentage CSS heights only work if the parent element has a height defined on it. By adding the above, you're giving .background's parents a height.

Update: based on OP's comment, here's how you would get the .background div to always appear to fill the viewport:

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

/* Fixed element that takes up entire viewport */
.background {
   position: fixed;
   z-index: 1;
   top: 0;
   left: 0;    

   width: 100%;
   height: 100%; 
}

/* Content that stacks above .background */
.content {
   position: relative;
   z-index: 2;
}

As .content grows larger than the viewport and the user scrolls, the fixed position of .background will keep it always in view.

And of course, a handy example.


All you need is:

body, html {
    height:100%
}

Then specify height:100%; any DIV you want to have full height.

BTW - 1000px wide is a bad unit to use. People with 1024 wide screens will get horizontal scrollbars. Better to stick to 980 or less. 960 is good because it can be divided by many factors.


I think this is what you're looking for.

http://jsfiddle.net/sg3s/GxRcp/

The key in this little example is the position: fixed; for .background so that it is kept in the screen while scrolling.

If you don't really want to do this and want the background to expand ARROUND the content just make it a normal / relatively positioned element, and wrap it arround .content...

If you give a more acurate description of the layout you're trying to create (and maybe why in such a way) we may be able to help you better.

Btw, in your example html there is an error, header should be head.


You should put bg into html or body elements as the first choices.

html { background: url("bg.jpg") no-repeat top center; }

or

body { background: url("bg.jpg") no-repeat top center; }

Fixed:

background: url("bg.jpg") no-repeat top center fixed; /* And bg will stay in fixed position */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜