Categories
JavaScript Answers

How to get div height with plain JavaScript?

Spread the love

Sometimes, we want to get div height with plain JavaScript.

In this article, we’ll look at how to get div height with plain JavaScript.

How to get div height with plain JavaScript?

To get div height with plain JavaScript, we use the clientHeight or offsetHeight properties.

For instance, we write

const clientHeight = document.getElementById("myDiv").clientHeight;
const offsetHeight = document.getElementById("myDiv").offsetHeight;

to get the div with getElementById.

Then we get the div’s height with the clientHeight and offsetHeight properties.

clientHeight includes the padding with the height.

And offsetHeight includes padding, scrollbar and border heights.

Conclusion

To get div height with plain JavaScript, we use the clientHeight or offsetHeight properties.

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 *