jQuery .width() Method — Get and Set Element Width

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is .width()?

.width() gets or sets the content width of matched elements (since jQuery 1.0). The getter returns a unitless pixel Number — ideal for math. It also works on $(window) and $(document).

Remember
$("div").width()           // Number  →  content width (px)
$("div").width(100)        // set 100px
$("div").width("50%")      // set with CSS units
$(window).width()          // viewport width
$(document).width()        // full page width

.width()     → Number (unitless)
.css("width") → String ("150px")
NeedUse
Content width (Number)$el.width()
Include padding$el.innerWidth()
Include padding + border$el.outerWidth()

How to Use .width()

Pick getter or setter, then choose the box edge you really need.

  1. Select the element(s) with jQuery.
  2. Call .width() with no args to read a unitless pixel Number.
  3. Pass a Number (pixels) or CSS string to set width on every match.
  4. Use the callback form when each element needs a different value.
PieceRequired?Role
jQuery selectionYesElement(s), window, or document
value / functionSetter onlyNew width (Number, string, or per-element fn)
Return—Getter → Number; setter → jQuery (chainable)

Pseudocode

Pseudocode
function width(selection, value):
    if value is missing:
        el = first matched element
        return contentWidth(el) as Number   // no "px"

    for each el in selection:
        if value is a function:
            newW = value(index, currentWidth)
        else:
            newW = value
        set CSS width of el to newW         // Number → pixels
    return selection

Cheat Sheet

Keep these facts handy while measuring horizontal size.

TopicDetail
Syntax.width() / .width(value) / .width(fn)
Since1.0 (callback setter: 1.4.1)
Getter returnsUnitless pixel Number (may be fractional)
MeasuresContent width (padding & border excluded)
box-sizingStill reports content width (extra work if border-box)
Window / document$(window).width() / $(document).width()
vs .css("width")Number vs string with units

Live Preview

Pick an action against the sample box and see what .width() returns or sets.

Browser demo of .width() — same idea as the examples below.

Result
$("#box").width()
→ 120

typeof result → "number"

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Get Element, Document, and Window Width

Official-style reads: paragraph, full page, and viewport.

jQuery
function showWidth(label, width) {
  console.log(label + ": " + width + "px");
}

showWidth("paragraph", $("p").width());
showWidth("document", $(document).width());
showWidth("window", $(window).width());
Try It Yourself

How It Works

  1. Element. $("p").width() returns content width as a Number.
  2. Document. $(document).width() is the full scrollable page width.
  3. Window. $(window).width() is the visible viewport width.
  4. Units. All three getters are unitless pixels — append "px" only when displaying.

Example 2 — Set Width with a Number

Pass a Number to set pixel width; chain other methods afterward.

jQuery
$("#box").width(50);

console.log($("#box").width());  // 50
Try It Yourself

How It Works

  1. Setter. .width(50) sets CSS width to 50 pixels on every match.
  2. Assumption. A bare Number means pixels; strings can use "50%", "2em", etc.
  3. Chain. The setter returns the jQuery object, so you can call .addClass() next.
  4. Verify. Call .width() again to read the new content width.

Example 3 — .width() vs .css("width")

See why the unitless Number is easier for math.

jQuery
var $box = $("#box");

console.log($box.width());           // 80  (Number)
console.log($box.css("width"));      // "80px" (String)
console.log($box.width() * 2);       // 160 — ready for math
Try It Yourself

How It Works

  1. Dimension helper. .width() strips units and returns a Number.
  2. CSS getter. .css("width") keeps the computed string (often with px).
  3. Math. Multiply the Number directly — no parseInt needed.
  4. Rule. Prefer .width() for calculations; use .css() for literal CSS values.

Example 4 — Callback Setter (Since 1.4.1)

Give each matched element a different width from its index.

jQuery
$(".bar").width(function (index) {
  return 50 + index * 20;
});

$(".bar").each(function (i) {
  console.log("bar " + i + ": " + $(this).width() + "px");
});
// bar 0: 50px
// bar 1: 70px
// bar 2: 90px
Try It Yourself

How It Works

  1. Callback. jQuery calls your function once per matched element.
  2. Args. First argument is the zero-based index; second is the current width.
  3. Return. Return a Number (or CSS string) to set that element’s width.
  4. Pattern. Great for shrinking tiles or staggered columns without a manual loop.

Edge Cases & Pitfalls

Check these before trusting .width() in layout code.

hidden

Hidden elements can report 0

Measure while the element is visible. Do not rely on temporary show/hide measurement.

box

border-box still returns content width

Since 1.8, jQuery may subtract padding/border to keep the content-width contract — that costs extra work. Use .css("width") if you only need the CSS property string.

fraction

Values may be fractional

Sub-pixel layouts can return numbers like 150.5. Round only when your UI needs integers.

tags

Avoid style / script tags

Calling .width() on those tags is unreliable and discouraged by the official API.

Browser Support

.width() has shipped since jQuery 1.0 (callback setter since 1.4.1) and works in all modern browsers with jQuery loaded.

✓ jQuery 1.0+

.width()

Core dimension API for content width — getter returns a unitless Number; setter accepts Number, string, or function.

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.width() Excellent

Bottom line: Use .width() for content-width math. Prefer .innerWidth() / .outerWidth() when padding or border must be included.

Performance Notes

ApproachCostBest for
.width()Layout readContent-width math
.css("width")Cheaper readCSS string / avoid border-box math
Repeated reads in a loopCan thrash layoutCache the Number first

Dimension getters force layout. Read once, reuse the Number, and avoid calling .width() inside tight animation frames unless needed.

Key Takeaways

  • Role: get/set content width as a unitless pixel Number.
  • Math: prefer .width() over parsing .css("width").
  • Box edges: use innerWidth / outerWidth when padding or border matters.
  • Safety: measure visible elements; expect fractional pixels.

One line: .width() is your content-width Number — get for math, set with a Number/string/callback, and switch to inner/outer when the box edge changes.

Frequently Asked Questions

.width() with no arguments returns the content width of the first matched element as a unitless pixel Number. .width(value) or .width(function) sets the CSS width on every matched element and returns the jQuery object for chaining.
.width() returns a Number like 400. .css('width') returns a string like '400px'. Use .width() when you need calculations; use .css('width') when you need the literal CSS value.
.width() is content width only (padding and border excluded). .innerWidth() includes horizontal padding. .outerWidth() includes padding and border (and optionally margin).
$(window).width() is the viewport width. $(document).width() is the full scrollable document width. Both return unitless Numbers.
No — .width() always reports content width. With box-sizing: border-box, jQuery may subtract padding and border (since 1.8) so the Number still matches the content box.
No. Dimensions are unreliable when the element or a parent is hidden. Measure while visible. Returned values may also be fractional on sub-pixel layouts.

Next: .innerHeight()

Learn height that includes padding on matched elements.

.innerHeight() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful