Categories
JavaScript Answers

How to get div height with plain JavaScript?

Spread the love

To get 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 element with getElementById.

And then we get the clientHeight or the offsetHeight to get the div’s height.

clientHeight includes padding.

offsetHeight includes padding, scrollBar and borders.

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 *