How do I retrieve an HTML element's actual width and height?
问题 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: You should use the .offsetWidth and .offsetHeight properties. Note they belong to the element, not .style . var width = document.getElementById('foo').offsetWidth; 回答2: Take a look at Element.getBoundingClientRect(). This method will return an object