2

How to set the height of this DIV so that it reaches the bottom of the viewport ? The DIV cannot be absolutely positioned ; it's in the flow.

<html>
   <body>
      Hello world...
      <div>xxx</div>
   </body>
</html>

If there is a CSS solution (which I doubt), it's great but using JS/jQuery is fine. CSS3 is fine too.

Blacksad
  • 14,906
  • 15
  • 70
  • 81
  • 2
    possible duplicate of [How to apply 100% height to div?](http://stackoverflow.com/questions/1818467/how-to-apply-100-height-to-div) – bfavaretto Mar 12 '12 at 15:47
  • could you tell us why you need it? maybe there's another way to achieve the same goal without inserting lots of javascript in your app and making the code harder to maintain, not to mention the annoying browser compatibility issues... – Castilho Mar 12 '12 at 15:51

3 Answers3

9

Using jQuery:

var h = $(window).height() - $('div').offset().top;
$('div').height(h);
calebds
  • 25,670
  • 9
  • 46
  • 74
0

Start with:

body, html { height:100% }

Then give your DIV height:100%

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

Use $(window).height() to get the actual height of the element

$("div").height($(window).height())
Starx
  • 77,474
  • 47
  • 185
  • 261