94

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,541
  • 28
  • 38
  • 38
lisovaccaro
  • 32,502
  • 98
  • 258
  • 410

7 Answers7

131
document.getElementById('banner-contenedor').clientWidth
xdazz
  • 158,678
  • 38
  • 247
  • 274
17

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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
10

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
  • 6,314
  • 1
  • 32
  • 58
  • 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>
Neuron
  • 5,141
  • 5
  • 38
  • 59
LF00
  • 27,015
  • 29
  • 156
  • 295
1

Try jQuery:

$("#banner-contenedor").width();
Reuben
  • 2,701
  • 6
  • 31
  • 47
-1

This jQuery worked for me:

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

This will get you the computed width

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

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

this way you can call the property string dynamically

ffdigital
  • 11
  • 5