0 votes
1 view
in Web Technology by (20.3k points)
edited by

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.

1 Answer

0 votes
by (36.7k points)

Try using the .offsetWidth and .offsetHeight properties. 

var width = document.getElementById('foo').offsetWidth;

Note: They belong to the element, not to the .style.