Too Cool for Internet Explorer

DIV layer with 100% height, the Javascript way   July 1st, 2009

When you create a div layer with 100% width and height, the height is always only the height of the browser window, not of the page content (which can be much higher).

To solve this, give the layer div an id and use the following JS code inside the layer div:

<script type="text/javascript">document.getElementById("yourlayerid").style.height = document.getElementsByTagName("body")[0].offsetHeight + "px";</script>

The code is very simple, it gets the height of the content (the body element) and gives it the layer div.

Tested in Opera, Firefox and *barf* MSIE (8).

Tags: , , , , , , ,
This entry was posted on Wednesday, July 1st, 2009 at 21:56 and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed.You can leave a response, or trackback from your own site.

5 Responses

February 7th, 2011 at 09:05
Vancouver Web Design Says:

Thanks, I needed to resize a div to the height of the window and this did the trick perfectly.

March 23rd, 2011 at 08:55
Linas Says:

Really great thing, solves all problems.

May 22nd, 2011 at 20:11
Mike Says:

Thank You, you are a god! I spend the whole day looking for this.

January 27th, 2012 at 19:16
pax Says:

grax thanks
solves all problems

October 12th, 2012 at 17:15
Andres Says:

Excellent trick, fixed my problem!

Leave a Reply