The first case is that the width and height are written in the style sheet, such as #div1{width:120px;}. In this case, the width cannot be obtained through #div1.style.width, but the width can be obtained through #div1.offsetWidth.
The second case is that width and height are written in the line, such as style="width:120px;", in which case, the width can be obtained through the above two methods.
Summary, because id.offsetWidth and id.offsetHeight ignore the style written in the style sheet or in the line, it is best to use these two attributes when we get the element width and height. Note that if the attributes written in the line style cannot be obtained through id.style.atrr.
Nowadays, front-end production rarely writes styles directly in style, but they are all written in style sheets. If the style you want to obtain does not correspond to (just #div1.style.width corresponds to #div1.offsetWidth), you can only obtain the attributes of the style sheet without using a browser. You can try searching for "JS Get Style Properties".
Code:
var o = document.getElementById("view");var h = o.offsetHeight; //Height var w = o.offsetWidth; //WidthThe above method to obtain the actual width and height of the Html element in js is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.