Categories
JavaScript Answers

How to find the width of a div using vanilla JavaScript?

Spread the love

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.

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 *