Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

100% Div height without scrollbar

Tags:

html

css

I have a two column layout:

<html>
 <head></head>
 <body>

    <div id="container">
        <div id="header"></div>
        <div id="sidewrapper"></div>
        <div id="contentwrapper"></div>
    </div>
 </body>
</html>

I want to have both sidebar and content be 100% in height but the most top container's min-height should be 100%.

I tried to solve it with the following css:

* {
    margin: 0;
    padding: 0;
}

html {
    font-family: Georgia, serif;
    color: #000; height:100%; min-height:100px;
}

body {
    background: #fff; height:100%; min-height:100px; overflow:hidden;   
}

#header {
width: 100%;
position: relative;
float: left;
height: 23px;
}

#container {
    position:relative;
    width: 100%; height:100%;
    margin: auto; background:blue;
}

#contentwrapper {
    float:left;
    background:red;
    position: relative;
    width: 700px; padding:0px; margin:0px;
    height: auto; 
}

#sidewrapper {
    float:left;
    position: relative;
    width: 159px; height:100%; 
    padding:0px; margin:0px;
}

...but I get a scrollbar because of the header's 23px height. I tried to solve it with overflow:hidden for the body element but I do not know if that is the right solution.

Any ideas?

like image 369
UpCat Avatar asked Nov 25 '10 08:11

UpCat


3 Answers

If my assumption I put forward in my comment to the question is right, then sidebar is 100% high, and on top of that you have a 23px header, so that causs your container to be 100% + 23px high.

In the future you will have in css calc() http://hacks.mozilla.org/2010/06/css3-calc/ . This will solve your problem.

Now, I guess you should calculate the height of the sidebar ( = height of container - 23px), by javascript.

like image 162
Gideon Avatar answered Nov 08 '22 17:11

Gideon


Make #header position absolute. This will cut that block out of the normal flow and you'll be able to make heights of other blocks equal to their parent.

#header {
    position: absolute;
    width: 100%; height: 23px;
    top: 0; left: 0;
}
#sidewrapper,
#contentwrapper {
    float: left;
    width: 50%;
    height: 100%;
}
#sidewrapper .content,
#contentwrapper .content {
    margin-top: 23px;
}
like image 32
vtambourine Avatar answered Nov 08 '22 17:11

vtambourine


The height of an element is compared with its father element. In your case, I recommend you specify the concrete width & height for "containter", because it'll be hard to pretend the size of the screen on many machines.

If you insists use percent, I recommend you use for both element, such as header 25% height and content 75% height.

like image 1
Hoàng Long Avatar answered Nov 08 '22 19:11

Hoàng Long