Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we change the html or body height using jquery?

I am trying to run this statement: $('body').height(100); but when I check the height again, I find it unchanged!

Is there any way to change the height of the <body>?

like image 797
theVincentTor Avatar asked Feb 23 '12 09:02

theVincentTor


People also ask

Which jQuery method is used to get or set the height of an HTML element?

jQuery height() Method The height() method sets or returns the height of the selected elements. When this method is used to return height, it returns the height of the FIRST matched element.

How do I set dynamic height in jQuery?

Answer: Use the JavaScript height() method You can set the height of a <div> box dynamically using the jQuery height() method.

How do I change the scroll height in jQuery?

$(document). ready(function() { //Get height of the div var iHeight = $("#dvContent"). height(); //Get ScrollHeight of the div var iScrollHeight = $("#dvContent"). prop("scrollHeight"); var msg = 'Height:' + iHeight + 'px & ScrollHeight:' + iScrollHeight + 'px'; $("span").


2 Answers

document body is a magical element and does not behave the way other HTML elements do. Your code:

$('body').height(100);

Is correct and it is same as:

$('body').css({height: 100});
$('body').css({height: '100px'});
$('body').attr({style: 'height: 100px'});

All result in this:

<body style="height: 100px;">

However, setting the height will not hide the area outside the 100px height invisible. You should wrap the body content inside a div and set its height instead:

<body>
    <div id="wrapper" style="height: 100px; overflow: hidden;">
        <!-- content -->
    </div>
</body>

jQuery solution would be:

$("body").wrapInner($('<div id="height-helper"/>').css({
    height: 100,
    overflow: "hidden"
}));
like image 99
Salman A Avatar answered Oct 03 '22 19:10

Salman A


You should actually use

$('body').css('height', '100'); and it will work.

like image 21
defau1t Avatar answered Oct 03 '22 19:10

defau1t