Connecting Tech Pros Worldwide Forums | Help | Site Map

Making a DIV height equal to page height

Newbie
 
Join Date: May 2009
Posts: 5
#1: May 18 '09
I want to create a div over the body which should take 100% height of the page.
I am defining html and body height is 100%. Also the div height is 100%.
still it's not working for me. Can some one please tell me what is wrong?
below is the code snippet.

Thanks.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3.  <HEAD>
  4.  
  5.   <script>
  6.   window.onload = pageLoad;
  7.   function pageLoad()
  8.             {
  9.               document.getElementsByTagName("html")[0].style.height = "100%"; 
  10.               document.body.style.height = "100%";
  11.               dv = document.createElement('div'); 
  12.               dv.style.position="absolute";
  13.               dv.style.top=0;
  14.               dv.style.left=0;
  15.                 dv.style.height="100%";
  16.                 dv.style.width="100%";
  17.                 dv.style.backgroundColor="red";
  18.                 dv.style.filter="alpha(opacity=70)";
  19.                 dv.style.opacity=.70; 
  20.     document.body.appendChild(dv);  
  21.            }
  22.     </script>
  23.  </HEAD>
  24.  
  25.  <BODY>
  26.   test<br><br><br><br><br><br><br>
  27.     test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  
  28.  </BODY>
  29. </HTML>

Needs Regular Fix
 
Join Date: Jun 2006
Posts: 424
#2: May 18 '09

re: Making a DIV height equal to page height


Try something like this-
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
  2. <html lang= "en">
  3. <head> 
  4. <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8"> 
  5.  
  6. <style type= "text/css"> 
  7. html, body{margin: 0; padding: 0}
  8. #scriptdiv{
  9.     position: absolute; top: 0; left: 0; width: 100%; 
  10.     background-color: red; z-index: 100
  11. }
  12. </style>
  13. <script type= "text/javascript"> 
  14. window.onload= function(){
  15.     var dv= document.createElement('div');
  16.     dv.id= 'scriptdiv';
  17.     dv.style.height= document.documentElement.clientHeight+'px';
  18.     document.body.appendChild(dv);
  19. }
  20. </script> 
  21. </head>
  22. <body> 
  23. test
  24. </body> 
  25. </html> 
Newbie
 
Join Date: May 2009
Posts: 5
#3: May 18 '09

re: Making a DIV height equal to page height


I tried your code but it gives me the same issue. DIV height does not stretch to the page height when content is longer than viewport.
In the following example, I don't get the red background till the end of the page.

Thanks.



Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">  
  2. <html lang= "en"> 
  3. <head>  
  4. <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8">  
  5.  
  6. <style type= "text/css">  
  7. html, body{margin: 0; padding: 0} 
  8. #scriptdiv{ 
  9.     position: absolute; top: 0; left: 0; width: 100%;  
  10.     background-color: red; z-index: 100 
  11. </style> 
  12. <script type= "text/javascript">  
  13. window.onload= function(){ 
  14.     var dv= document.createElement('div'); 
  15.     dv.id= 'scriptdiv'; 
  16.     dv.style.height= document.documentElement.clientHeight+'px'; 
  17.     document.body.appendChild(dv); 
  18. </script>  
  19. </head> 
  20. <body>  
  21.  test<br><br><br><br><br><br><br>
  22.     test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>   test<br><br><br><br><br><br><br>
  23.  
  24. </body>  
  25. </html>
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: May 19 '09

re: Making a DIV height equal to page height


Perhaps what you need is the offsetHeight, though I can't remember the browser incompatibilities in this area.
Member
 
Join Date: May 2009
Location: Goregaon, मुंबई IN :)
Posts: 85
#5: May 20 '09

re: Making a DIV height equal to page height


Do you want something like this:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <style type="text/css">
  4.     #dvMain{
  5.         width:100%;
  6.         height:100%;
  7.         background-color:steelblue;
  8.     }
  9.     body{
  10.         width:100%;
  11.         height:100%;
  12.         margin: 0;
  13.         padding: 0;
  14.     }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="dvMain">
  19.     Hello world
  20. </div>
  21. </body>
  22. </html>
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#6: May 21 '09

re: Making a DIV height equal to page height


I think position:fixed, with left, right, top, bottom set to 0px and overflow:auto should work. But I'm not sure if this is what you actually require.
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#7: May 22 '09

re: Making a DIV height equal to page height


Is this what you want??
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">  
  2. <html lang= "en"> 
  3. <head>  
  4. <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8">  
  5.  
  6. <style type= "text/css">  
  7. html, body{
  8.     margin: 0; 
  9.     padding: 0;
  10.     height:100%;
  11. .someStyle{ 
  12.     width: 100%; 
  13.     height: 100%; 
  14.     background-color: green; 
  15.  
  16. </style> 
  17.  
  18.  
  19. </head> 
  20. <body>  
  21. <div class="someStyle">
  22.   hello world!
  23. </div>
  24. </body>  
  25. </html>
Note that I'm kind of cheating in this code snippet.

The problem that you're facing is that the <html> and <body> tags have a height that is equal to the content. Therefore, if you have a <div> as content and set it's height to 100% it will only be as tall as the content within it.

So in the above snippet I've set the <html> and <body> tags to have a height of 100%.

Now the reason I say it's a cheat is because when you do this you will see a scroll bar in some browsers.

You need to do as Acoder has suggested here:
Quote:

Originally Posted by acoder View Post

Perhaps what you need is the offsetHeight, though I can't remember the browser incompatibilities in this area.

You need to determine the height of the browser window and set your <div>'s height to that value.

Here is a JavaScript function that will find and return the height of the window:

Expand|Select|Wrap|Line Numbers
  1. function BrowserHeight() {
  2.     var theHeight;
  3.     if (window.innerHeight) {
  4.         theHeight = window.innerHeight;
  5.     }
  6.     else if (document.documentElement && document.documentElement.clientHeight) {
  7.         theHeight = document.documentElement.clientHeight;
  8.     }
  9.     else if (document.body) {
  10.         theHeight = document.body.clientHeight;
  11.     }
  12.     return theHeight;
  13. }
So a much better solution would be:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">  
  2. <html lang= "en"> 
  3. <head>  
  4. <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8">  
  5.  
  6. <style type= "text/css">  
  7. html, body{
  8.     margin: 0; 
  9.     padding: 0;
  10.  
  11. .someStyle{ 
  12.     width: 100%; 
  13.     height: 100%; 
  14.     background-color: green; 
  15.     z-index: 100; 
  16.  
  17. </style> 
  18.  
  19. <script type="text/javascript">
  20. function SetHeightOfDiv(){
  21.     var theDiv = document.getElementById('myDiv');
  22.     theDiv.style.height = BrowserHeight()+"px";
  23. }
  24.  
  25. function BrowserHeight() {
  26.     var theHeight;
  27.     if (window.innerHeight) {
  28.         theHeight = window.innerHeight;
  29.     }
  30.     else if (document.documentElement && document.documentElement.clientHeight) {
  31.         theHeight = document.documentElement.clientHeight;
  32.     }
  33.     else if (document.body) {
  34.         theHeight = document.body.clientHeight;
  35.     }
  36.     return theHeight;
  37. }
  38. </script>
  39. </head> 
  40. <body onload="SetHeightOfDiv()">  
  41. <div id="myDiv" class="someStyle">
  42.   hello world!
  43. </div>
  44. </body>  
  45. </html>
Newbie
 
Join Date: May 2009
Posts: 5
#8: Jul 8 '09

re: Making a DIV height equal to page height


Thanks to all of you guys. I have resolved this issue using 'scrollHeight' property. Pleas see the code below.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
  2. <HTML> 
  3.  <HEAD> 
  4.  
  5.   <script> 
  6.   window.onload = pageLoad; 
  7.   function pageLoad() 
  8.             { 
  9.             pageHeight = document.getElementById('page').scrollHeight;
  10.               document.getElementsByTagName("html")[0].style.height = "100%";  
  11.               document.body.style.height = "100%"; 
  12.               dv = document.createElement('div');  
  13.               dv.style.position="absolute"; 
  14.               dv.style.top=0; 
  15.               dv.style.left=0; 
  16.                 dv.style.height= pageHeight+"px"; 
  17.                 dv.style.width="100%"; 
  18.                 dv.style.backgroundColor="red"; 
  19.                 dv.style.filter="alpha(opacity=70)"; 
  20.                 dv.style.opacity=.70;  
  21.     document.body.appendChild(dv);   
  22.            } 
  23.     </script> 
  24.  </HEAD> 
  25.  
  26.  <BODY id="page"> 
  27.   test<br><br><br><br><br><br><br> 
  28.     test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>  test<br><br><br><br><br><br><br>   
  29.  </BODY> 
  30. </HTML>
Reply

Tags
body, div, height, page