Suppose I have the following html, and no CSS
<div>
here is some content in this div. it stretches it out
<br />and down too!
</div>
Now I want to get the actual pixel width and height that the browser has rendered this div as.
Can that be done with JS?
Thank you.
Try getting a reference to your div and reading the
offsetWidthandoffsetHeightproperties:offsetWidth/Heightcumulatively measures the element’s borders, horizontal padding, vertical scrollbar (if present, if rendered) and CSS width. It’s the pixel values of the entire space that the element uses in the document. I think it’s what you want.If that is not what you meant, and you’d rather only the element’s
widthandheight(i.e. excluding padding, margin, etc) trygetComputedStyle:The values above will be the final, computed pixel values for the
widthandheightcss style properties (including values set by a<style>element or an external stylesheet).Like all helpful things, this won’t work in IE.
You say you are using jQuery. Well it’s trivial now, and works cross-browser:
With jQuery you don’t need the first part of this answer, it’s all taken care of for ya 😉