Categories
JavaScript Answers

How to retrieve an HTML element’s actual width and height with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *