How do I retrieve an HTML element's actual width and height?

前端 未结 14 1806
后悔当初
后悔当初 2020-11-22 02:29

Suppose that I have a

that I wish to center in the browser\'s display (viewport). To do so, I need to calculate the width and height of the &l
14条回答
  •  野的像风
    2020-11-22 02:56

    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.

提交回复
热议问题