jQuery .width() Method — Get and Set Element Width
Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview
Definition
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")
Pick getter or setter, then choose the box edge you really need.
Select the element(s) with jQuery.
Call .width() with no args to read a unitless pixel Number.
Pass a Number (pixels) or CSS string to set width on every match.
Use the callback form when each element needs a different value.
Piece
Required?
Role
jQuery selection
Yes
Element(s), window, or document
value / function
Setter only
New 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
Reference
Cheat Sheet
Keep these facts handy while measuring horizontal size.
Topic
Detail
Syntax
.width() / .width(value) / .width(fn)
Since
1.0 (callback setter: 1.4.1)
Getter returns
Unitless pixel Number (may be fractional)
Measures
Content width (padding & border excluded)
box-sizing
Still reports content width (extra work if border-box)
Window / document
$(window).width() / $(document).width()
vs .css("width")
Number vs string with units
Try it
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.
Sample box
Result
$("#box").width()
→ 120
typeof result → "number"
Code
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.
Callback. jQuery calls your function once per matched element.
Args. First argument is the zero-based index; second is the current width.
Return. Return a Number (or CSS string) to set that element’s width.
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.width()Excellent
Bottom line: Use .width() for content-width math. Prefer .innerWidth() / .outerWidth() when padding or border must be included.
Analysis
Performance Notes
Approach
Cost
Best for
.width()
Layout read
Content-width math
.css("width")
Cheaper read
CSS string / avoid border-box math
Repeated reads in a loop
Can thrash layout
Cache the Number first
Dimension getters force layout. Read once, reuse the Number, and avoid calling .width() inside tight animation frames unless needed.
Remember
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.
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.