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

前端 未结 14 1688
后悔当初
后悔当初 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:59

    According to MDN: Determining the dimensions of elements

    offsetWidth and offsetHeight return the "total amount of space an element occupies, including the width of the visible content, scrollbars (if any), padding, and border"

    clientWidth and clientHeight return "how much space the actual displayed content takes up, including padding but not including the border, margins, or scrollbars"

    scrollWidth and scrollHeight 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.

提交回复
热议问题