473,808 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making a DIV height equal to page height

5 New Member
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>
May 17 '09 #1
7 14795
mrhoo
428 Contributor
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> 
May 18 '09 #2
ad08
5 New Member
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>
May 18 '09 #3
acoder
16,027 Recognized Expert Moderator MVP
Perhaps what you need is the offsetHeight, though I can't remember the browser incompatibiliti es in this area.
May 19 '09 #4
NitinSawant
270 Contributor
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>
May 20 '09 #5
hsriat
1,654 Recognized Expert Top Contributor
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.
May 20 '09 #6
Frinavale
9,735 Recognized Expert Moderator Expert
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:
@acoder
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>
May 22 '09 #7
ad08
5 New Member
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>
Jul 7 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3969
by: marek | last post by:
if table body has specified height set, which would be taller than the summa of the specified heighted cells, should the cells heights be adjusted to be more taller, so the summa height will equal the height specified in table body? -- Marek Mänd Estonia, Tallinn
1
1869
by: John | last post by:
Hi again, I appreciate all the advice I've gotten so far. My question this time is whether there's a way to make the row height equal to the column width when you don't know in advance what you'll be using. The content of each cell is text.
4
2440
by: Robert McClenon | last post by:
I am having a minor but annoying problem with some reports in Access 2003. (I had the same problem with Access 97, so it isn't version-dependent.) Some of the reports that I originally developed with the Report Wizard but have since edited now print so that there is a blank page after each actual page. I assume that this is because the page size is slightly larger than the Page Setup for the LaserJet. I assume that I need to adjust some...
1
3222
by: automation | last post by:
There is a truncation error of my Web Application using PHP 5.04, MySQL 5.022, and HTML(IE 6.0) whereby the MySQL Result Set is being truncated on the HTML page, even though the CSS Div Page Height is set to 'auto' which is supposed to grow the HTML page height to the size necessary to display the contents within the page. The query count is correct, but the MySQL Result Set is being truncated on the html page. This happens for MySQL Results on...
10
10504
Ajm113
by: Ajm113 | last post by:
Making a History Page for BIG Sites Intro: Ok, let's say after a while your website has grown massive. We're talking search engine, forum and video hosting -- you've got a LOT of content. And you are wondering, "Why do I need yet another feature for my big site?" Well, some people can become forgetful every time they see content on your site, and let's suppose that one day they needed to work on a good php script for their class and they...
1
1358
by: jaiwin | last post by:
If any one know the dynamic div height according to the page height.
10
4280
pradeepjain
by: pradeepjain | last post by:
Hello guys, I am working on a site where in i have a container of width 90% within which all header footer and content area comes.I have a small problem with footer that is when i have content more the footer goes down which is ok.but when i have very small content say 5 lines the footer comes up this is the main problem.when the content is less then a page height i want the footer to be at the bottom of the page..is there...
0
1470
by: SLauren | last post by:
Hi, I am creating a crystal report using ASP.Net in Visual Studio 2005. I am getting more space below the report. Can anyone please help how to reduce the page height? Following is the whole stuff which the .aspx file contains: <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" Height="1039px" ReportSourceID="CrystalReportSource1" Width="901px" BestFitPage="False" /> ...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.