Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

html - CSS Layout Help - Stretch div to bottom of page

I'm trying to create a layout with a 'header' area which contains a logo and some links, and then a content area which needs to extend to the bottom of the page. This is where I'm getting stuck.

I've surrounded the header and content with a container div which has a height of 100%, this works fine. But I can't then get the content div to stretch to the bottom of the container div as giving it a minimum height of 100% appears to take the height from the page body, so I end up with a scroll bar due to the space taken up at the top of the page by the header.

Here's a wireframe which hopefully makes what I'm trying to achieve a bit clearer...

Mockup

Here is a quick CSS example, this works, apart from there always being a scrollbar at which appears to be the height of the header area...

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
    color: #fff;
}
body {
    background-color: #000;
}
#container {
    position: relative;
    margin: 0 auto;
    width: 1000px;
    min-height: 100%;
}
#header {
    padding-top: 26px;
}
#logo {
    width: 194px;
    height: 55px;
    margin: 0 auto;
    background-color: #fff;
}
#content {
    margin-top: 10px;
    position: absolute;
    width: 1000px;
    min-height: 100%;
    background-color: #fff;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

http://jsfiddle.net/CZayc/

this works by wrapping the header and body in a div to push footer down

index.html

<div id="wrap">
    <div id="header">
        header
    </div>
    <div id="main">
        main<br/>main<br/>main<br/>main<br/>main<br/>main<br/>
    </div>
</div>
<div id="footer">
    footer
</div>

style.css

body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td { 
    margin:0;
    padding:0;
}
#header {
    border-top:20px solid #fff;
    height: 33px;
    line-height: 33px;
    text-align: center;
    background-color: green;
}
html { height: 100%; }
body { height: 100%; width: 90%; margin: auto; }
#wrap { min-height: 100%;background-color:gray;}
#main {
    overflow: auto;
    padding-bottom: 53px; /* must be same height as the footer */
    background-color: red;
    height: 90%
}
#footer {
    position: relative;
    margin-top: -53px; /* negative value of footer height */
    height: 33px;
    line-height: 33px;
    border-bottom:20px solid #fff;
    text-align: center;
    background-color:blue;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...