I always thought elements that had display:none CSS style returned 0 for height() and width().
But in this example:
HTML
<div id="target" style="display:none;">
a
</div>
CSS
alert($("#target").height());
they don’t: http://jsfiddle.net/Gts6A/2/
How come? I’ve seen 0 come back plenty of times in the past.
If an element has an
offsetWidthof0(jQuery is considering this “hidden”), checked here, then it attempts to figure out what the height should be. It sets the following properties on the element viajQuery.swap()for the duration of the check:position: "absolute"visibility: "hidden"display: "block"Then it gets the height, via
getWidthOrHeight(...)which adds border/padding if necessary viaaugmentWidthOrHeight(...)depending on the box model, and reverts all of the above properties to their former values.So basically it’s taking the element, showing it out of the flow of the document, getting the height, then hiding it again, all before the UI thread updates so you never see this happen. It’s doing this so
.height()/.width()works, even on elements that are currently hidden, as long as their parents are visible…so you can run.height()/.width(), without doing the show/hide trick it’s doing in your code, it’s handled within.height()and.width().