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")
CallWhat 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

ParameterDescription
Step 1Select the element(s) with jQuery.
Step 2Call .width() with no args to read a unitless pixel Number.
Step 3Pass a Number (pixels) or CSS string to set width on every match.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters 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.

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.

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.

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.

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() Method — Height Including Padding

Learn height that includes padding on matched elements.

.innerHeight() Method — Height Including Padding tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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