admin管理员组

文章数量:1321449

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.

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.

Share Improve this question asked Mar 12, 2012 at 15:46 BlacksadBlacksad 15.4k16 gold badges74 silver badges81 bronze badges 2
  • 2 possible duplicate of How to apply 100% height to div? – bfavaretto Commented Mar 12, 2012 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 patibility issues... – Castilho Commented Mar 12, 2012 at 15:51
Add a ment  | 

3 Answers 3

Reset to default 9

Using jQuery:

var h = $(window).height() - $('div').offset().top;
$('div').height(h);
  • DEMO

Start with:

body, html { height:100% }

Then give your DIV height:100%

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

$("div").height($(window).height())

本文标签: javascriptSet DIV height so that it reaches the bottom of the viewportStack Overflow