height: 100%
in CSS obviously doesn't work. So is there any other CSS or JavaScript/jQuery solutions to this problem? Please advise.

- 165
- 1
- 2
- 5
-
1It works, but the user would have to put the browser in full screen mode for the div to actually take up the entire space. – Dan Mar 03 '11 at 03:40
-
`height: 100%` in CSS works exactly as described in the spec. here: http://www.w3schools.com/css/pr_dim_height.asp – simon Mar 03 '11 at 03:42
-
1Even if you can technically solve it by using 100% height also on html, body and wrapper divs I think it's sometimes useful to set 100% height on a div based on canvas height minus other known heights, and check again on resize. Mostly because after making numerous web sites I have found the pure CSS solution to have some flaws. I prefer to base it on that and improve with JS. – plebksig Mar 03 '11 at 08:19
8 Answers
'Let's say your problem element is a <div>
. If you make sure your <div>
s height has something to reference to, almost all your problems will disappear:
#my_div
{
height: 100%; /* Won't work. What is 100% of an unknown/unset value? */
}
Make sure the <div>
's parents have a set height too. I usually do this (well, not exactly, but you get the idea):
#my_div, #parent_of_the_div, body, html
{
height: 100%; /* This works, but it will show scrollbars if the body
or html elements have padding or margins. */
}

- 289,723
- 53
- 439
- 496
-
2This only works if you're willing to restrict your entire layout to the height of the monitor – thefreeman Nov 26 '12 at 12:55
So you want a div to be the height of the screen? It's kind of non-obvious, but css height is the correct approach. The trick is you need to have the html and body elements also take up the full height of the page, otherwise the div is taking up 100% of nothing. The best way I've found to do this is:
html {
height: 100%;
}
body {
height: 100%;
}
#contentDiv {
min-height: 100%;
}

- 2,201
- 16
- 14
No Javascript required,becouse CSS3 has some new values for sizing things relative to the current viewport size: vw, vh, and vmin
1vw = 1% of viewport width
1vh = 1% of viewport height
1vmin = 1vw or 1vh, whichever is smaller
1vmax = 1vw or 1vh, whichever is larger
so you can write it on your style :
#contentDiv {
height: 100vh;
}

- 3,205
- 1
- 17
- 10
Pure css
#container {
position:absolute;
top:0;
right:0;
bottom:0;
left:0;
}
Good Luck
Or javacript Jquery:
Ready (not innerHeight in ie8):
$(document).ready( function(){
var heightwindow = $(document).height();
$('#container').css('height', heightwindow+'px');
});
resize window :
$(window).resize(function() {
var heightwindow = $(document).height();
$('#container').css('height', heightwindow+'px');
});

- 695
- 7
- 16
With jQuery, you could use:
$('div.class').css('height', $(window).height()+'px');

- 483
- 5
- 9
There are a few options you may find useful:
vh (viewport height)
vw (viewport width)
vmin (viewport minimum length)
vmax (viewport maximum length)
#container{
height: 100vh;
background: black;
}

- 21
- 1
My answer builds on jonwayne's because there wasn't much explanation.
You cannot use css to get the value of a users monitor, but you can do it via javascript. So the trick is to add javascript to the page load event which will set the height based on the browser window height. Using jQuery, you can do this with the following snippet
// jquery shorthand for onLoad event
$(function() {
// Set the css height property of #div_to_resize to the css
// height property of the browser window
$('#div_to_resize').css('height',$(window).css('height'));
}
You can also optionally attach to the resize event of the browser to reset the height if the window is resized. Combined with the previous snippet it would be
// We extracted this to a function since we reference it more then once
function matchHeight() {
$('#div_to_resize').css('height',$(window).height);
}
// jQuery shorthand for document.onload
$(function() {
matchHeight();
//On the resize event, call matchHeight()
$(window).resize(matchHeight);
});

- 1,035
- 11
- 13