jQuery width() method is used to return or set the width of matched element.

To return width: When this method is used to return the width, it returns the width of first matched element.

To set width:When this method is used to set the width, it sets the width for every matched element.

This method is one of a jQuery dimension.

List of jQuery dimension:

  • width()
  • height()
  • innerWidth()
  • innerHeight()
  • outerWidth()
  • outerHeight()


To return the width:


To set the width:


To set the width using a function:


Parameters of jQuery width() method

Parameter Description
Value It is a mandatory parameter. It is used for setting width. It specifies the width in px, em, pt etc. The default value of jQuery width() method is px.
Function(index, currentwidth) It is an optional parameter. It specifies a function that provides the new width of selected element.
  • Index:IIt provides the index position of the element in the set.
  • currentwidth:It provides the current width of the selected element.

Example of jQuery width() method

Let's take an example to demonstrate the effect of jQuery width() method.

To return width:

<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        alert("Width of div: " + $("div").width());
<div style="height:100px;width:200px;padding:10px;margin:3px;border:1px solid blue;background-color:lightpink;"></div><br>
<button>Execute the jQuery width() method to return width</button>

jQuery width() example 2

To set width:

<!DOCTYPE html>
<html lang="en">
  <meta charset="utf-8">
  <title>width demo</title>
  div {
    width: 100px;
    height: 80px;
    float: left;
    margin: 5px;
    cursor: pointer;
  .mod {
    background: green;
    cursor: default;
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
var modWidth = 70;
$( "div" ).one( "click", function() {
  $( this ).width( modWidth ).addClass( "mod" );
  modWidth -= 10;
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +