jQuery .width() Method — Get and Set Element Width
Beginner
5 min read
Updated: Nov 2026
3 examples
CSS
Definition
.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).
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("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")
Call
What it does
.width()
Learn jQuery .width(): get or set content width as a unitless pixel Number, compare with .css('width'), window and document widths, callback setter, examples, pitfalls, and FAQs.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Select the element(s) with jQuery.
Step 2
Call .width() with no args to read a unitless pixel Number.
Step 3
Pass a Number (pixels) or CSS string to set width on every match.
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefined
Getters usually return a value from the first matched element, or undefined when nothing applies.
jQuery Examples
Example 1 — Get Element, Document, and Window Width
Official-style reads: paragraph, full page, and viewport.
Dimension helper..width() strips units and returns a Number.
CSS getter..css("width") keeps the computed string (often with px).
Math. Multiply the Number directly — no parseInt needed.
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.