How do I retrieve an HTML element's actual width and height?
How do I retrieve an HTML element's actual width and height?
Question
Suppose that I have a <div>
that I wish to center in the browser's display (viewport). To do so, I need to calculate the width and height of the <div>
element.
What should I use? Please include information on browser compatibility.
Accepted Answer
You should use the .offsetWidth
and .offsetHeight
properties.
Note they belong to the element, not .style
.
var width = document.getElementById('foo').offsetWidth;
Function .getBoundingClientRect()
returns dimensions and location of element as floating-point numbers after performing CSS transforms.
> console.log(document.getElementById('id').getBoundingClientRect())
DOMRect {
bottom: 177,
height: 54.7,
left: 278.5,
right: 909.5,
top: 122.3,
width: 631,
x: 278.5,
y: 122.3,
}
Read more… Read less…
Take a look at Element.getBoundingClientRect()
.
This method will return an object containing the width
, height
, and some other useful values:
{
width: 960,
height: 71,
top: 603,
bottom: 674,
left: 360,
right: 1320
}
For Example:
var element = document.getElementById('foo');
var positionInfo = element.getBoundingClientRect();
var height = positionInfo.height;
var width = positionInfo.width;
I believe this does not have the issues that .offsetWidth
and .offsetHeight
do where they sometimes return 0
(as discussed in the comments here)
Another difference is getBoundingClientRect()
may return fractional pixels, where .offsetWidth
and .offsetHeight
will round to the nearest integer.
IE8 Note: getBoundingClientRect
does not return height and width on IE8 and below.*
If you must support IE8, use .offsetWidth
and .offsetHeight
:
var height = element.offsetHeight;
var width = element.offsetWidth;
Its worth noting that the Object returned by this method is not really a normal object. Its properties are not enumerable (so, for example, Object.keys
doesn't work out-of-the-box.)
More info on this here: How best to convert a ClientRect / DomRect into a plain Object
Reference:
.offsetHeight
: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight.offsetWidth
: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth.getBoundingClientRect()
: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
NOTE: this answer was written in 2008. At the time the best cross-browser solution for most people really was to use jQuery. I'm leaving the answer here for posterity and, if you're using jQuery, this is a good way to do it. If you're using some other framework or pure JavaScript the accepted answer is probably the way to go.
As of jQuery 1.2.6 you can use one of the core CSS functions, height
and width
(or outerHeight
and outerWidth
, as appropriate).
var height = $("#myDiv").height();
var width = $("#myDiv").width();
var docHeight = $(document).height();
var docWidth = $(document).width();
Just in case it is useful to anyone, I put a textbox, button and div all with the same css:
width:200px;
height:20px;
border:solid 1px #000;
padding:2px;
<input id="t" type="text" />
<input id="b" type="button" />
<div id="d"></div>
I tried it in chrome, firefox and ie-edge, I tried with jquery and without, and I tried it with and without box-sizing:border-box
. Always with <!DOCTYPE html>
The results:
Firefox Chrome IE-Edge
with w/o with w/o with w/o box-sizing
$("#t").width() 194 200 194 200 194 200
$("#b").width() 194 194 194 194 194 194
$("#d").width() 194 200 194 200 194 200
$("#t").outerWidth() 200 206 200 206 200 206
$("#b").outerWidth() 200 200 200 200 200 200
$("#d").outerWidth() 200 206 200 206 200 206
$("#t").innerWidth() 198 204 198 204 198 204
$("#b").innerWidth() 198 198 198 198 198 198
$("#d").innerWidth() 198 204 198 204 198 204
$("#t").css('width') 200px 200px 200px 200px 200px 200px
$("#b").css('width') 200px 200px 200px 200px 200px 200px
$("#d").css('width') 200px 200px 200px 200px 200px 200px
$("#t").css('border-left-width') 1px 1px 1px 1px 1px 1px
$("#b").css('border-left-width') 1px 1px 1px 1px 1px 1px
$("#d").css('border-left-width') 1px 1px 1px 1px 1px 1px
$("#t").css('padding-left') 2px 2px 2px 2px 2px 2px
$("#b").css('padding-left') 2px 2px 2px 2px 2px 2px
$("#d").css('padding-left') 2px 2px 2px 2px 2px 2px
document.getElementById("t").getBoundingClientRect().width 200 206 200 206 200 206
document.getElementById("b").getBoundingClientRect().width 200 200 200 200 200 200
document.getElementById("d").getBoundingClientRect().width 200 206 200 206 200 206
document.getElementById("t").offsetWidth 200 206 200 206 200 206
document.getElementById("b").offsetWidth 200 200 200 200 200 200
document.getElementById("d").offsetWidth 200 206 200 206 200 206
According to MDN: Determining the dimensions of elements
offsetWidth
and offsetHeight
return the "total amount of space an element occupies, including the width of the visible content, scrollbars (if any), padding, and border"
clientWidth
and clientHeight
return "how much space the actual displayed content takes up, including padding but not including the border, margins, or scrollbars"
scrollWidth
and scrollHeight
return the "actual size of the content, regardless of how much of it is currently visible"
So it depends on whether the measured content is expected to be out of the current viewable area.
You only need to calculate it for IE7 and older (and only if your content doesn't have fixed size). I suggest using HTML conditional comments to limit hack to old IEs that don't support CSS2. For all other browsers use this:
<style type="text/css">
html,body {display:table; height:100%;width:100%;margin:0;padding:0;}
body {display:table-cell; vertical-align:middle;}
div {display:table; margin:0 auto; background:red;}
</style>
<body><div>test<br>test</div></body>
This is the perfect solution. It centers <div>
of any size, and shrink-wraps it to size of its content.