How do you find the current width of a <div>
in a cross-browser compatible way without using a library like jQuery?
-
134+1 for the "no jQuery" comment :) – Zlatko Jun 13 '13 at 17:25
-
4Possible duplicate of [Understanding offsetWidth, clientWidth, scrollWidth and -Height, respectively](http://stackoverflow.com/questions/21064101/understanding-offsetwidth-clientwidth-scrollwidth-and-height-respectively) (which, indeed, has been asked much later, but still has a **much more elaborate answer**). – Oct 04 '16 at 15:39
8 Answers
document.getElementById("mydiv").offsetWidth

- 338,112
- 86
- 474
- 445
-
12
-
161
-
1When myDiv had no CSS rule but had "width" and "height" defined in the tag, offsetWidth returned the parent width. Not a problem I just added the CSS rule and it worked fine. – rob Apr 20 '14 at 10:29
-
-
7@nim if your div has `display: none` or is not part of the document, it will always have zero offset height. – Andy E May 04 '16 at 07:48
-
3[This answer](https://stackoverflow.com/questions/21064101/understanding-offsetwidth-clientwidth-scrollwidth-and-height-respectively) has a detailed visual explanation of the difference between `offsetWidth` and `clientWidth`. – Tony O'Hagan Mar 05 '18 at 00:58
You can use clientWidth
or offsetWidth
Mozilla developer network reference
It would be like:
document.getElementById("yourDiv").clientWidth; // returns number, like 728
or with borders width :
document.getElementById("yourDiv").offsetWidth; // 728 + borders width

- 1
- 1

- 1,609
- 12
- 7
All Answers are right, but i still want to give some other alternatives that may work.
If you are looking for the assigned width (ignoring padding, margin and so on) you could use.
getComputedStyle(element).width; //returns value in px like "727.7px"
getComputedStyle allows you to access all styles of that elements. For example: padding, paddingLeft, margin, border-top-left-radius and so on.

- 62,887
- 36
- 269
- 388

- 211
- 2
- 6
Another option is to use the getBoundingClientRect function. Please note that getBoundingClientRect will return an empty rect if the element's display is 'none'.
var elem = document.getElementById("myDiv");
if(elem) {
var rect = elem.getBoundingClientRect();
console.log(rect.width);
}

- 1,277
- 1
- 11
- 21
You can also search the DOM using ClassName. For example:
document.getElementsByClassName("myDiv")
This will return an array. If there is one particular property you are interested in. For example:
var divWidth = document.getElementsByClassName("myDiv")[0].clientWidth;
divWidth
will now be equal to the the width of the first element in your div array.

- 573
- 1
- 5
- 18

- 146
- 1
- 10
Actually, you don't have to use document.getElementById("mydiv")
.
You can simply use the id of the div, like:
var w = mydiv.clientWidth;
or
var w = mydiv.offsetWidth;
etc.

- 326
- 2
- 5
The correct way of getting computed style is waiting till page is rendered. It can be done in the following manner. Pay attention to timeout on getting auto
values.
function getStyleInfo() {
setTimeout(function() {
const style = window.getComputedStyle(document.getElementById('__root__'));
if (style.height == 'auto') {
getStyleInfo();
}
// IF we got here we can do actual business logic staff
console.log(style.height, style.width);
}, 100);
};
window.onload=function() { getStyleInfo(); };
If you use just
window.onload=function() {
var computedStyle = window.getComputedStyle(document.getElementById('__root__'));
}
you can get auto
values for width and height because browsers does not render till full load is performed.

- 541
- 1
- 4
- 8
call below method on div or body tag onclick="show(event);" function show(event) {
var x = event.clientX;
var y = event.clientY;
var ele = document.getElementById("tt");
var width = ele.offsetWidth;
var height = ele.offsetHeight;
var half=(width/2);
if(x>half)
{
// alert('right click');
gallery.next();
}
else
{
// alert('left click');
gallery.prev();
}
}

- 446
- 5
- 14