.innerWidth()

Get the current computed inner width (including padding but not border) for the first element in the set of matched elements or set the inner width of every matched element.

.innerWidth()Returns: Number

Description: Get the current computed inner width for the first element in the set of matched elements, including padding but not border.

This method returns the width of the element, including left and right padding, in pixels. If called on an empty set of elements, returns undefined (null before jQuery 3.0).

This method is not applicable to window and document objects; for these, use .width() instead.

Figure 1 - Illustration of the measured width

Additional Notes:

  • The number returned by dimensions-related APIs, including .innerWidth(), may be fractional in some cases. Code should not assume it is an integer. Also, dimensions may be incorrect when the page is zoomed by the user; browsers do not expose an API to detect this condition.
  • The value reported by .innerWidth() is not guaranteed to be accurate when the element or its parent is hidden. To get an accurate value, ensure the element is visible before using .innerWidth(). jQuery will attempt to temporarily show and then re-hide an element in order to measure its dimensions, but this is unreliable and (even when accurate) can significantly impact page performance. This show-and-rehide measurement feature may be removed in a future version of jQuery.

Example:

Get the innerWidth of a paragraph.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>innerWidth demo</title>
  <style>
  p {
    margin: 10px;
    padding: 5px;
    border: 2px solid #666;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<p>Hello</p>
<p></p>
 
<script>
var p = $( "p" ).first();
$( "p" ).last().text( "innerWidth:" + p.innerWidth() );
</script>
 
</body>
</html>

Demo:

.innerWidth( value )Returns: jQuery

Description: Set the CSS inner width of each element in the set of matched elements.

  • version added: 1.8.0.innerWidth( value )

    • value
      Type: String or Number
      A number representing the number of pixels, or a number along with an optional unit of measure appended (as a string).
  • version added: 1.8.0.innerWidth( function )

    • function
      Type: Function( Integer index, Number width ) => String or Number
      A function returning the inner width (including padding but not border) to set. Receives the index position of the element in the set and the old inner width as arguments. Within the function, this refers to the current element in the set.

When calling .innerWidth("value"), the value can be either a string (number and unit) or a number. If only a number is provided for the value, jQuery assumes a pixel unit. If a string is provided, however, any valid CSS measurement may be used for the width (such as 100px, 50%, or auto). Note that in modern browsers, the CSS width property does not include padding, border, or margin, unless the box-sizing CSS property is used.

If no explicit unit is specified (like "em" or "%") then "px" is assumed.

Example:

Change the inner width of each div the first time it is clicked (and change its color).

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>innerWidth demo</title>
  <style>
div {
width: 60px;
padding: 10px;
height: 50px;
float: left;
margin: 5px;
background: red;
cursor: pointer;
}
.mod {
background: blue;
cursor: default;
}
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<div>d</div>
<div>d</div>
<div>d</div>
<div>d</div>
<div>d</div>
 
<script>
var modWidth = 60;
$( "div" ).one( "click", function() {
$( this ).innerWidth( modWidth ).addClass( "mod" );
modWidth -= 8;
});
</script>
 
</body>
</html>

Demo:

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jquery.com/innerWidth