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.
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.
Here is the code for WKWebView what determines a height of specific Dom element (doesn't work properly for whole page)
According to MDN: Determining the dimensions of elements
offsetWidth
andoffsetHeight
return the "total amount of space an element occupies, including the width of the visible content, scrollbars (if any), padding, and border"clientWidth
andclientHeight
return "how much space the actual displayed content takes up, including padding but not including the border, margins, or scrollbars"scrollWidth
andscrollHeight
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.
element.offsetWidth and element.offsetHeight should do, as suggested in previous post.
However, if you just want to center the content, there is a better way of doing so. Assuming you use xhtml strict DOCTYPE. set the margin:0,auto property and required width in px to the body tag. The content gets center aligned to the page.
Take a look at
Element.getBoundingClientRect()
.This method will return an object containing the
width
,height
, and some other useful values:For Example:
I believe this does not have the issues that
.offsetWidth
and.offsetHeight
do where they sometimes return0
(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
: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/getBoundingClientRectYou should use the
.offsetWidth
and.offsetHeight
properties. Note they belong to the element, not.style
.var width = document.getElementById('foo').offsetWidth;
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:
This is the perfect solution. It centers
<div>
of any size, and shrink-wraps it to size of its content.