Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a DIV fill the remaining vertical space of the browser window?

Tags:

html

css

I have this simplified code:

This is a line of text<br/>
<div style="background-color: orange; height: 100%">And this is a div</div>

The div height ends up being 100% of the height of the browser window client space, which summed up with the height of the text line, is more than the window height, so you have to scroll.

How can I make the div height so it takes the height of the browser window minus the line of text?

Or, in other words, how can I make the div take all the free space vertically regarding what other DOM objects already occupy?

like image 344
Petruza Avatar asked Feb 12 '11 20:02

Petruza


People also ask

How do I make a div fill remaining vertical space?

Another way of making a <div> fill the remaining space is to use the CSS position property. Just set the position to “absolute” to stretch the <div>.

How do I make a div fit the height of my screen?

Syntax: To set a div element height to 100% of the browser window, it can simply use the following property of CSS: height:100vh; Example: HTML.

How do I extend a div to the bottom of the page?

If you need to make a <div> element extend to the bottom and fill the page, try the following. Use viewport units and set the height and width of the <body> element to “100vh” and “100vw” respectively. Both the margin and padding of the <html> and <body> elements must be set to 0.


2 Answers

I met the same problem too. Here is the solution I found:

<style>
.container{
    position: relative;
    height: 100%;
}
.top-div{
    /* height can be here. if you want it*/
}
.content{
    position:absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 1em; /* or whatever height of upper div*/
    background: red;
}
</style>
<div class="container">
    <div class="top-div">This is a line of text</div>
    <div class="content">And this is a div</div>
</div>

source - http://www.codingforums.com/archive/index.php/t-142757.html

like image 181
Okendoken Avatar answered Oct 26 '22 21:10

Okendoken


Ultimately, you will want to have a container. "overflow: hidden" will hide anything that overflows the container. If we didn't use that then we would see the problem you mentioned above about "...is more than the window height, so you have to scroll".

  <div id="container" style="color:white;height:500px;background-color:black;overflow:hidden;">
    This is the container
    <div style="height:100%;background-color:orange;">
      This div should take the rest of the height (of the container).
    </div>
  </div>

Example with overflow hidden: http://jsbin.com/oxico5

Example without overflow hidden: http://jsbin.com/otaru5/2

like image 29
Shaz Avatar answered Oct 26 '22 20:10

Shaz