86

I have this element:

<div style="width: 100%; height: 10px;"></div>

I want to get it's width in pixels. I just tried this:

document.getElementById('banner-contenedor').style.width

Which returns 100%. Is it possible to actually get the element's width in pixels with JavaScript?

Jason Aller
  • 3,475
  • 28
  • 40
  • 37
lisovaccaro
  • 30,232
  • 96
  • 246
  • 398

7 Answers7

115
document.getElementById('banner-contenedor').clientWidth
xdazz
  • 154,648
  • 35
  • 237
  • 264
16

You want to get the computed width. Try: .offsetWidth

(I.e: this.offsetWidth='50px' or var w=this.offsetWidth)

You might also like this answer on SO.

GitaarLAB
  • 14,134
  • 11
  • 55
  • 76
9

Not a single answer does what was asked in vanilla JS, and I want a vanilla answer so I made it myself.

clientWidth includes padding and offsetWidth includes everything else (jsfiddle link). What you want is to get the computed style (jsfiddle link).

function getInnerWidth(elem) {
    return parseFloat(window.getComputedStyle(elem).width);
}

EDIT: getComputedStyle is non-standard, and can return values in units other than pixels. Some browsers also return a value which takes the scrollbar into account if the element has one (which in turn gives a different value than the width set in CSS). If the element has a scrollbar, you would have to manually calculate the width by removing the margins and paddings from the offsetWidth.

function getInnerWidth(elem) {
    var style = window.getComputedStyle(elem);
    return elem.offsetWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight) - parseFloat(style.borderLeft) - parseFloat(style.borderRight) - parseFloat(style.marginLeft) - parseFloat(style.marginRight);
}

With all that said, this is probably not an answer I would recommend following with my current experience, and I would resort to using methods that don't rely on JavaScript as much.

Domino
  • 5,681
  • 32
  • 55
  • This not working/have issues if width css is `100%`; – towry Mar 30 '20 at 03:32
  • @towry Care to explain what's the issue so I can correct my answer if needed? I wrote this five years ago. – Domino Mar 30 '20 at 12:18
  • I just had the issue days ago, Suppose the initial css is `width: 100%`, Then I use `getComputedStyle` to get the width in pixels, but it always return empty value, but in a timeout callback, it can get a correct pixel number value. – towry Mar 31 '20 at 07:07
  • @towry, at a guess your problem is calling getComputedStyle before the DOM is rendered. I have had this problem myself which is when it returns nothing. The solution is to wait until the DOM is ready. I do this by attaching a function to `document.addEventListener('readystatechange', myfunction);` My function checks that `document.readyState === 'complete'` before I do anything with `getComputedStyle`. Never a problem since. – Bernd Wechner Aug 24 '21 at 10:56
3

You can use offsetWidth. Refer to this post and question for more.

console.log("width:" + document.getElementsByTagName("div")[0].offsetWidth + "px");
div {border: 1px solid #F00;}
<div style="width: 100%; height: 10px;"></div>
LF00
  • 24,667
  • 25
  • 136
  • 263
1

Try jQuery:

$("#banner-contenedor").width();
Reuben
  • 2,583
  • 5
  • 28
  • 47
-1

This jQuery worked for me:

$("#banner-contenedor").css('width');

This will get you the computed width

http://api.jquery.com/css/

Mark
  • 681
  • 8
  • 12
-4
yourItem.style['cssProperty']

this way you can call the property string dynamically

ffdigital
  • 11
  • 5