jQuery API

.width()

Contents:

.width() Returns: Integer

Description: Get the current computed width for the first element in the set of matched elements.

  • version added: 1.0.width()

The difference between .css(width) and .width() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .width() method is recommended when an element's width needs to be used in a mathematical calculation.

This method is also able to find the width of the window and document.

$(window).width();   // returns width of browser viewport
$(document).width(); // returns width of HTML document

Note that .width() will always return the content width, regardless of the value of the CSS box-sizing property.

Example:

Show various widths. Note the values are from the iframe so might be smaller than you expected. The yellow highlight shows the iframe body.

<!DOCTYPE html>
<html>
<head>
  <style>
  body { background:yellow; }
  button { font-size:12px; margin:2px; }
  p { width:150px; border:1px red solid; }
  div { color:red; font-weight:bold;  }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button id="getp">Get Paragraph Width</button>
  <button id="getd">Get Document Width</button>
  <button id="getw">Get Window Width</button>

  <div>&nbsp;</div>
  <p>
    Sample paragraph to test width
  </p>
<script>
function showWidth(ele, w) {
  $("div").text("The width for the " + ele + 
                " is " + w + "px.");
}
$("#getp").click(function () { 
  showWidth("paragraph", $("p").width()); 
});
$("#getd").click(function () { 
  showWidth("document", $(document).width()); 
});
$("#getw").click(function () { 
  showWidth("window", $(window).width()); 
});

</script>

</body>
</html>

Demo:

.width( value ) Returns: jQuery

Description: Set the CSS width of each element in the set of matched elements.

  • version added: 1.0.width( value )

    valueAn integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string).

  • version added: 1.4.1.width( function(index, width) )

    function(index, width)A function returning the width to set. Receives the index position of the element in the set and the old width as arguments. Within the function, this refers to the current element in the set.

When calling .width("value"), the value can be either a string (number and unit) or a number. If only a number is provided for the value, jQuery assumes a pixel unit. If a string is provided, however, any valid CSS measurement may be used for the width (such as 100px, 50%, or auto). Note that in modern browsers, the CSS width property does not include padding, border, or margin, unless the box-sizing CSS property is used.

If no explicit unit is specified (like "em" or "%") then "px" is assumed.

Note that .width("value") sets the width of the box in accordance with the CSS box-sizing property. Changing this property to border-box will cause this function to change the outerWidth of the box instead of the content width.

Example:

Change the width of each div the first time it is clicked (and change its color).

<!DOCTYPE html>
<html>
<head>
  <style>
div { width: 70px; height: 50px; float:left; margin: 5px;
        background: red; cursor: pointer; }
.mod { background: blue; cursor: default; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
  <div>d</div>
  <div>d</div>
  <div>d</div>
  <div>d</div>
  <div>d</div>

<script>
(function() {
  var modWidth = 50;
  $("div").one('click', function () {
    $(this).width(modWidth).addClass("mod");
  modWidth -= 8;
  });
})();
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .width() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .width()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • http://clearcreativegroup.com Doug

    i was just wondering if there was a way animate this procedure like perhaps

    $(“#iframewin”).width(700,”slow”);
    });
    });

    • http://www.learningjquery.com/ Karl Swedberg

      $(‘#iframewin’).animate({width: 700}, ‘slow’);

  • Anonymous

    in IE
    I had a problem where .width() was returning 1 for an image that I knew was 700. The problem turned out to be that it was a problem with the width attribute.
    I am using php to serve images and {WIDTH} was supposed to be replaced with the width of the image, but the php was not working.

    So the html in the browser looked like this

    and the script

    alert($(“imgHold”).width());

    in all browser but IE, 700 was returned but IE returned 1 because of the erroneous width attribute.

    Took me a while to figure out,… Should have validated code.

    Thought It might help others

  • tails

    for hidden elements, this method returns 0, though checking direction with .css('width') may work.

  • http://twitter.com/sidewaysmilk Justin Force

    I was having trouble with getting the correct value from a DIV in IE. It turns out it's because I'm using filter:progid:DXImageTransform.Microsoft.Shadow to get a drop shadow. When you read the width, the added pixels from the drop shadow are included, so $('#box').width($('#box').width()) makes the box larger by 2x the shadow strength.

    I'm using box-shadow on EOMB and DirectX filters in IE, so my fix is to check $.support.opacity, and if it's false, get the shadow size with

    $('#box').css('filter').match(/shadowS+strength=(d+)/i)[1]

    then double that and subtract it from the reported width.

    Thought I'd share in case anybody else is pulling their hair out over this.

  • http://shirkey.myopenid.com/ shirkey

    Maybe, add hash before id element:

    alert($(“#imgHold”).width());

  • http://twitter.com/Jon47 Jonz

    Another difference between .width() and .css('width') is that **in IE7 and IE8** .width() will always return the calculated width, whereas .css('width') may return something like “auto”.

    I haven't tested in other versions of IE; Chrome, Safari, Firefox, et al seem to function properly.

  • Sreevalli 5636

    it helped me alot

  • Stephen

    What value does .width() return for an element with display:none in jQuery 1.4.4? Does it now return the actual value? If so, that would help greatly, because sometimes, we want to hide something and do some “prep” work before revealing the element(s). In jQuery 1.4.3, it would return 0, forcing us to hide the element off-screen rather than using display:none.

    • Brian

      I am also wondering about this…

  • KeithHopkin

    Anyone else having problems getting the width of a TD? This is only returning the width that the inner content of a TD occupies unless I put a DIV inside with an inline width which sort of defeats the purpose for me.

    • 7ochem

      See .outerWidth()

  • Jebokokot Dreveny

    soske mange, chamulangma :)

  • http://pulse.yahoo.com/_JZR5XFFNDDIVQU6PVNVYH4X4AY Adrian

    Hi! I have an div wrapper and some div-s inside of it.
    Can I calculate all div-s width inside the wrapper and give the total width to the wrapper?
    Thank you!

  • 7ochem

    See .outerWidth()

  • Phil

    Is this function meant to return always an Integer? I have a case where it returns some float value. So it's maybe needed to change the set return Type from Integer to Float on this page, or some kind of error occurs.

  • http://twitter.com/cloudkiller cloudkiller

    When trying to get the width of an image with something like the following:

    var imageWidth=$(“#myDiv img”).width();

    The width has to be specified in the image code for Chrome/Safari to be able to correctly detect the width. If the image code looks like this:

    <img src=”my_super_cool_image.png”>

    Firefox and IE will correctly detect the width but Chrome and Safari will return a 0. When you add width=”XXX” then Chrome/Safari will return the XXX value.

  • http://twitter.com/lungdisc lungdisc

    hey

  • .oisyn

    The selector “myDiv” is referencing an HTML element with tag name “myDiv”, which probably doesn't exist as myDiv isn't a valid HTML tag. If “myDiv” is it's id, then you should be using $(“#myDiv”)