To find the width of a div using vanilla JavaScript, we use the offsetWidth
or clientWidth
property.
For instance, we write
const offsetWidth = document.getElementById("myDiv").offsetWidth;
const clientWidth = document.getElementById("myDiv").clientWidth;
to select the div with getElementById
.
And then we get the width of the div with the offsetWidth
or clientWidth
properties.
offsetWidth
includes border width, and clientWidth
does not.