How to find the width of a div using vanilla JavaScript?

前端 未结 8 1246
遥遥无期
遥遥无期 2020-11-27 10:19

How do you find the current width of a

in a cross-browser compatible way without using a library like jQuery?

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 11:00

    All Answers are right, but i still want to give some other alternatives that may work.

    If you are looking for the assigned width (ignoring padding, margin and so on) you could use.

    getComputedStyle(element).width; //returns value in px like "727.7px"
    

    getComputedStyle allows you to access all styles of that elements. For example: padding, paddingLeft, margin, border-top-left-radius and so on.

提交回复
热议问题