javascript - How can you get the height of an element with height:auto? - Stack Overflow

I'm trying to set the height of one element to the height of another by using javscript, and I

I'm trying to set the height of one element to the height of another by using javscript, and I've tried using the jquery height() method but it doesn't work because one of the element's has height set to auto:

#content
{
    background-color:#F2F2F2;
    width:1000px;
    margin:0px auto;
    text-align:left;
    min-height:900px;
    height:auto;
    border-top:5px solid #032F76;
    border-bottom:5px solid #032F76;
    overflow:auto;
}

So is there any easy way to get the height of an element who's height is set to auto?

I'm trying to set the height of one element to the height of another by using javscript, and I've tried using the jquery height() method but it doesn't work because one of the element's has height set to auto:

#content
{
    background-color:#F2F2F2;
    width:1000px;
    margin:0px auto;
    text-align:left;
    min-height:900px;
    height:auto;
    border-top:5px solid #032F76;
    border-bottom:5px solid #032F76;
    overflow:auto;
}

So is there any easy way to get the height of an element who's height is set to auto?

Share Improve this question asked Feb 25, 2012 at 22:30 Gavin SellersGavin Sellers 6641 gold badge15 silver badges27 bronze badges 1
  • Are you talking about height of block itself or height of its contents? – Marat Tanalin Commented Feb 25, 2012 at 22:35
Add a ment  | 

4 Answers 4

Reset to default 3

jQuery's .height() method still returns the numerical height value even if it's set to auto. BUT make sure you take margin, padding and borders into consideration.

I ran a test with your CSS just to make sure and .height() works fine.
http://jsfiddle/ysMge/8/

jQuery has 3 height calculating functions that take the margin, padding and border values into account differently.

Please review:

$("#content").height()
$("#content").outerHeight()
$("#content").innerHeight()

http://api.jquery./height/
http://api.jquery./outerHeight/
http://api.jquery./innerHeight/

You can use getComputedStyle:

var elem = document.getElementById('content');
var height = window.getComputedStyle(elem,null).getPropertyValue('height');

Which should return the puted height, in pixels, of the element.

A somewhat contrived JS Fiddle example.

Just use this code

$('#content').height();

heres a sample of my code:

$(document).ready(function() {
  CenterFrameSizer();
});

function CenterFrameSizer(){
  // Sets the left box and the center box the same height.
  if ( $("#leftbox").height() > $("#contentbox").height() ) {
    $("#contentbox").height($("#leftbox").height());
  } else {
    $("#leftbox").height($("#contentbox").height());
  }
}

function GalleryResize(){
  if ( $("#leftbox").height() > $("#gallery").height() ) {
    $("#gallery").height($("#leftbox").height());
  } else {
    $("#leftbox").height($("#gallery").height());
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744968855a4603844.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信