It is outlined in the jQuery API :
Note that .hight () will always return the height of the content, regardless of the value of the CSS CSS dimension property. As in jQuery 1.8, this may require getting the CSS height property plus the window size and then subtracting any potential border and padding for each element when the element has a box size: border-box. To avoid this punishment, use .css ("height") rather than .height ().
I tried the following example
<div id='wrapper' class='boxA'> <div class='boxB'> Test </div> </div>
with CSS
.boxA{ padding: 20px; background-color: yellow; box-sizing: border-box; margin: 50px; } .boxB{ height: 50px; background-color: red; }
According to the jQuery API, I was sure that
$('#wrapper').height($('#wrapper').height());
will set boxA height to 50px (since the content height is 50 pixels), but I found that the height was set to 90 pixels. However, if I use
$('#wrapper').css('height', $('#wrapper').height()+"px");
boxA height gets 50px and therefore shrinks. Why doesn't the first method produce 50px height?
In addition, the team
$('#wrapper').height($('#wrapper').css('height'));
will set the height of boxA to 130px, but $('#wrapper').css('height') will return 90. What happened here?
You can find these examples in this jFiddle .
source share