How to get a div height with plain JavaScript?

Spread the love

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

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

How to get a div height with plain JavaScript?

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

For instance, we write

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

to select the div with getElementById.

Then we get its height with clientHeight or offsetHeight.

clientHeight includes padding.

And offsetHeight includes the heights of the padding, scrollBar and borders.

Conclusion

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

Leave a Reply

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