Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a div fill the remaning vertical space using css

Tags:

html

css

I am attempting to make a standard website layout with a header, a navigation bar a body (on the right of the navigation bar) and a footer.

Now I have so far done this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <style type="text/css" media="screen">

            .header {
                float: top;
                width: 100%;
                height: 75px;
            }

            .navbar {
                float: left;
                width: 20%;
                height: 100%;
                height: 100%;
                min-height:100%;
                overflow: scroll;
            }

            .body {
                float: right;
                width: 80%;
                height: 100%;
                min-height:100%;
                overflow: scroll;
            }
            .footer {
                float: bottom;
                width: 100%;
            }
        </style>
    </head>
    <body>
        <div class="header"> Header </div>
        <div class="navbar"> Nav Bar </div>
        <div class="body"> Body </div>
        <div class="footer"> Footer</div>
    </body>
</html>

which produces this:

enter image description here

Now if we check the CSS:

.navbar {

    float: left;
    width: 20%;
    height: 100%;
    min-height: 100%;
    overflow: scroll;
}

.body {
    float: right;
    width: 80%;
    height: 100%;
    min-height: 100%;
    overflow: scroll;
}

As you can see I have tried to set the height and min-height of both the body and nav bar to fill the remaining vertical space i.e:

enter image description here

Yet it doesnt affect it. However if I do height: 500px it resizes like expected (of course this now wont be very good practice as different screen sizes etc would show a different portion or view of the page):

enter image description here

So basically I am asking how would I be able to make the divs fill the vertical space that's left over without using some hard-coded value i.e 100px rather I would want to do it in percentages thus the page will look the same on all browsers

like image 212
David Kroukamp Avatar asked Apr 10 '13 10:04

David Kroukamp


People also ask

How do you calculate available height in CSS?

Use min-height: 100vh on body , then set the parent ( body in this case) to display: flex; flex-direction: column and use flex-grow: 1 on . content for it to use the available space.

How do I fill a div with remaining space?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

How do you make an image fill the space of the div?

You want the image to fill the container's height. A good way of doing this is setting the container to position: relative; and the child image to position: absolute; with height: 100%; . That is one way for forcing the child element to fill it's parent's height.


1 Answers

add this code to your body,html:

body,html{
  height:100%;
}

and make your navbar <div id="navbar"> instead of <div class="navbar"> then add height: 100%; to your navbar

#navbar{
  height:100%
// rest of your code
}

Same to your content call it something like content, because body is already used.

#content{
  height:100%
// rest of your code
}

now all the divs will have a height of 100% so the full browser height.

EDIT: your full code would look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <style type="text/css" media="screen">

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

            #header {
                width: 100%;
                height: 75px;
            }

            #navbar {
                float: left;
                width: 20%;
                height: 100%;
                min-height:100%;
                overflow: scroll;
            }

            #content {
                float: right;
                width: 80%;
                height: 100%;
                min-height:100%;
                overflow: scroll;
            }
            #footer {
                width: 100%;
            }
        </style>
    </head>
    <body>
        <div id="header"> Header </div>
        <div id="navbar"> Nav Bar </div>
        <div id="content"> Body </div>
        <div id="footer"> Footer</div>
    </body>
</html>
like image 149
Kees Sonnema Avatar answered Oct 21 '22 13:10

Kees Sonnema