北城已荒凉 2008-11-16 19:15 采纳率: 100%
浏览 399
已采纳

如何检索 HTML 元素的实际宽度和高度?

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.

转载于:https://stackoverflow.com/questions/294250/how-do-i-retrieve-an-html-elements-actual-width-and-height

  • 写回答

12条回答 默认 最新

  • MAO-EYE 2008-11-16 19:35
    关注

    You should use the .offsetWidth and .offsetHeight properties. Note they belong to the element, not .style.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(11条)

报告相同问题?