Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly set the 100% DIV height to match document/window height?

Tags:

I have a wrapper positioned to center with an y-repeated background image:

<body>     <div id="wrapper">         ...some content     </div> </body>  #wrapper{ width: 900px; margin: 0 auto 0 auto; background-image: url(image.jpg) 250px 0px repeat-y; } 

Problem: when the window size is higher than the wrapper's height inherited from its content, the image obviously doesn't repeat on y-axis all the way to the bottom of the window.

I've used jQuery to dynamically apply inline CSS style to the wrapper according to actual document height (when DOM is ready and on window resize event):

$(function(){     $('#wrapper').css({'height':($(document).height())+'px'});     $(window).resize(function(){         $('#wrapper').css({'height':($(document).height())+'px'});     }); }); 

The problem with this approach is that every time one extends the window height to a size larger than the previously resized largest size, the document height extends by this difference, essentially making the wrapper DIV infinite if you keep resizing the window infinitely and have a infinite vertical display space.

On a typical 30" inch display with 1600px height, when user opens the website with a window height 1000px and wrapper is 800px high, the jQuery above sets the height to 1000px tiling the background image correctly. At this point, once the user extends the window size to e.g 1400px, the 1400px is a new document size "remembered default" and doesn't update itself even if the user resizes his window back to the original 1000px height, adding 400px to the scrollbar height at the bottom.

How to fix this?

UPDATE: (window).height doesn't seem to work, because window height is a viewport height. When your viewport is smaller than the content and you scroll it, the wrapper always stays the size of the viewport and doesn't extend to the bottom of the current viewport position.

like image 798
DominiqueBal Avatar asked Feb 09 '12 15:02

DominiqueBal


People also ask

How do I set the height of a div to 100?

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.

What does height 100% do CSS?

height: 100% gives the element 100% height of its parent container. height: auto means the element height will depend upon the height of its children.

How Div height is equal to body height?

Answer: Set the 100% height for parents too And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.


1 Answers

I figured it out myself with the help of someone's answer. But he deleted it for some reason.

Here's the solution:

  1. remove all CSS height hacks and 100% heights
  2. Use 2 nested wrappers, one in another, e.g. #wrapper and #truecontent
  3. Get the height of a browser viewport. IF it's larger than #wrapper, then set inline CSS for #wrapper to match the current browser viewport height (while keeping #truecontent intact)
  4. Listen on (window).resize event and ONLY apply inline CSS height IF the viewport is larger than the height of #truecontent, otherwise keep intact

    $(function(){     var windowH = $(window).height();     var wrapperH = $('#wrapper').height();     if(windowH > wrapperH) {                                     $('#wrapper').css({'height':($(window).height())+'px'});     }                                                                                    $(window).resize(function(){         var windowH = $(window).height();         var wrapperH = $('#wrapper').height();         var differenceH = windowH - wrapperH;         var newH = wrapperH + differenceH;         var truecontentH = $('#truecontent').height();         if(windowH > truecontentH) {             $('#wrapper').css('height', (newH)+'px');         }      })           }); 
like image 72
DominiqueBal Avatar answered Oct 04 '22 01:10

DominiqueBal