jQuery .width(): Measure and Set Content Width
jQuery’s .width() reads the first matched element’s content width as a number in pixels or sets the content width of all matched elements. The measurement excludes padding, borders, and margins. Use .innerWidth() or .outerWidth() when you need a different box boundary.
Read or set content width
The official jQuery .width() documentation describes a numeric getter. Even if a stylesheet declares a percentage width, the browser can return a calculated content width with a fractional pixel value.
Use jQuery .css() to read the CSS width property as a string. With box-sizing: border-box, the CSS width includes padding and borders. .width() still reports the content width.
const $box = $("#box");
const width = $box.width();
$box.width(300);
$box.width("50%");
A number such as 300 sets a width in pixels. Pass a string such as "50%" for another unit. When a width should keep adapting to its container, set the percentage and limits in CSS.
Compare padding, border, and margin measurements
Use jQuery .innerWidth() to measure through the padding and jQuery .outerWidth() to measure through the border. .outerWidth(true) also includes the horizontal margins. The table shows the demo values before applying the maximum.
| Call | What it measures | Initial result | After setting content width to 300 |
|---|---|---|---|
.width() |
Content box | 250 |
300 |
.innerWidth() |
Content and padding | 290 |
340 |
.outerWidth() |
Content, padding, and borders | 300 |
350 |
.outerWidth(true) |
Border box and margins | 320 |
370 |
.css("width") |
Computed CSS width string | 300px |
350px |
The initial border-box width is 300px. Subtract 40px of horizontal padding and 10px of borders to get 250px of content. The margin measurement adds 10px on each side.
Set and limit the width in a browser
Save the code as UTF-8 in example.html and open it in a browser. The demo loads jQuery 4.1.0 from an external server, so it needs an internet connection. On a narrow screen, the example box can scroll within its own area.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Measure box widths with jQuery .width()</title>
<style>
body { max-width: 32rem; margin: 1rem auto; padding: 0 1rem; font: 1rem/1.5 system-ui; }
.viewport { overflow-x: auto; }
#box { box-sizing: border-box; width: 300px; padding: 20px; border: 5px solid #835b00; margin: 10px; }
button { font: inherit; max-width: 100%; }
#output { white-space: pre-wrap; overflow-wrap: anywhere; }
</style>
<script src="https://code.jquery.com/jquery-4.1.0.min.js"></script>
</head>
<body>
<div class="viewport"><div id="box">Box to measure</div></div>
<button id="resize" type="button">Set content width to 300px</button>
<button id="limit" type="button">Limit width to 320px</button>
<p>Current width measurements</p>
<pre id="output" aria-live="polite"></pre>
<script>
$(function () {
const $box = $("#box");
function report() {
const widths = {
".width()": $box.width(),
".innerWidth()": $box.innerWidth(),
".outerWidth()": $box.outerWidth(),
".outerWidth(true)": $box.outerWidth(true),
'.css("width")': $box.css("width")
};
$("#output").text(JSON.stringify(widths, null, 2));
}
$("#resize").on("click", function () {
$box.css("max-width", "").width(300);
report();
});
$("#limit").on("click", function () {
$box.css("max-width", "320px");
report();
});
report();
});
</script>
</body>
</html>
Press Set content width to 300px. .width() now returns 300, .innerWidth() returns 340, and .outerWidth() returns 350. The CSS width is 350px because the border box adds 50px to the content.
Then press Limit width to 320px. With border-box, the border box is capped at 320px, leaving 270px for content. The width including margins is 340px. Read the measurement again after setting it if you need the actual size.
Check what you are measuring
A getter reads the first match, and a setter changes all matches. If a result is unexpected, check which elements matched first. Measurements can be unreliable when an element or parent is hidden. Measure visible elements when possible and allow for browser zoom and fractional pixels.
$(window).width() gives the viewport width, while $(document).width() gives the document width. Choose the target according to whether you need the content box, the visible area, or the whole document.




