I would like to get the window height with jQuery/JavaScript (which I'm not good at) and apply it to a few divs (3-4) with class 'slide'. Why I don't want to use height: 100%;
in CSS is because some elements get covered by the ones that have height to 100%: screenshot (I could make the footer 100% high but that would be a mess) and also because the height changes with zooming in/out (i.e. instead of staying for example 1080p, it changes to 100% and still fills window when zooming out).
So then I found some answers here on stackoverflow, and some people were suggesting something like this: (from: here)
$(document).ready(function(){
$(".slide").css("height", $(window).height());
});
$(window).resize(function(){
$('.slide').css('height':($(window).height());
});
I've edited it a bit.
I read a bunch of other posts on the same problem, and tried everything, but nothing seems to work for me. Even made a new set of files to test this alone, look at it on JSFiddle.
Thanks in advance!
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.
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.
Values. Defines the height as an absolute value. Defines the height as a percentage of the containing block's height. The browser will calculate and select a height for the specified element.
The code in your question is correct, but the code in your fiddle is wrong: You've used a :
where you want a ,
(and separately, you haven't included jQuery in your fiddle).
The code should be
$(document).ready(function(){
$('.slide').css('height', $(window).height());
// Comma, not colon ----^
});
$(window).resize(function(){
$('.slide').css('height', $(window).height());
// Comma, not colon ----^
});
Updated Fiddle (I added a background to .slide so you could see it's the full height)
Note that in the fiddle you end up with scrollbars, because of the default padding on body
. But I assume in your real site/app, you're dealing with that in CSS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With