views:

116

answers:

2

Why in the following code .height() returns 95 rather than 100, while .width() returns 200 as expected ? I work with Firefox 3.6.3.

HTML:

<table><tr>
   <td id="my"></td>
</tr></table>
<div id="log"></div>

CSS:

#my {
   border: 5px solid red;
}

JS:

$("#my").width(200).height(100);
$("#log").append("Width = " + $("#my").width() + "<br />");
$("#log").append("Height = " + $("#my").height());

I tried .outerWidth() and .outerHeight() and also .innerWidth() and .innerHeight(), but none of them returns the expected result: code example

But, if I set position: absolute it looks much better !

Can anyone explain this behavior ?

A: 

I am not sure about this.. I also find it rather strange.. This is my guess.

The border eats up into the actual height and is neglected by jquery while calculating height

ZX12R
The modern box model doesn't do that. Maybe it's an IE6 issue... but shouldn't it be 190 and 90 if that's the case?
Matchu
yes you have a valid point
ZX12R
the question is why width and height behaves differently. I would expect both to include or not include the border/padding/margins.
Misha Moroshko
+7  A: 

There are a few jQuery methods for calculating height and width. Try using outerHeight()

Excerpt from jQuery Docs: http://api.jquery.com/outerHeight/

.outerHeight( [ includeMargin ] )

includeMargin - A Boolean indicating whether to include the element's margin in the calculation.

http://api.jquery.com/innerHeight/

.innerHeight()

This method returns the height of the element, including top and bottom padding, in pixels.

Edit: Setting height() on the td-element is adjusted to include the default padding (1px). The computed dimensions of are actually...

alt text

You should set the default padding to 0px to avoid these issues.

table td {
    padding: 0;
}

Edit 2: It appears to be a browser issue (probably something related to the rendering engine's method of calculating a table's dimensions). The effects of this behavior will vary across browers. You should find an alternate, table-less, solution using divs.

John Himmelman
outer and inner functions doesn't help here. See updated question.
Misha Moroshko
I tried to set padding=0 of `td`, but it didn't help. see the updated question.
Misha Moroshko
This is exactly what I decided to do: to find another solution using `div`s :)
Misha Moroshko