Sometimes, we want to retrieve an HTML element’s actual width and height with JavaScript.
In this article, we’ll look at how to retrieve an HTML element’s actual width and height with JavaScript.
How to retrieve an HTML element’s actual width and height with JavaScript?
To retrieve an HTML element’s actual width and height with JavaScript, we use its offsetWidth
property.
For instance, we write
const width = document.getElementById("foo").offsetWidth;
to select the element with getElementById
.
Then we use its offsetWidth
property to get its width in pixels.
Conclusion
To retrieve an HTML element’s actual width and height with JavaScript, we use its offsetWidth
property.