Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery width() method

jQuery width() method

jQuery HTML/CSS methods jQuery HTML/CSS methods

Example

Return

The width of an element:

$

(

"

button

"

)

.

click

(

function

(

)

{

alert

(

$

(

"

div

"

)

.

width

(

)

)

;

}

)

;

Try it »


Definition and Usage

The width() method sets or returns the width of the selected elements.

When this method is used toReturnwidth, it returns the width of the first matched element.

When this method is used toSettingswidth, it sets the width of all matched elements.

As shown in the image below, this method does not include padding, border, or margin.

jQuery Dimensions


Related methods:

  • height() - Sets or returns the height of an element
  • innerWidth() - Returns the width of an element (including padding)
  • innerHeight() - Returns the height of an element (including padding)
  • outerWidth() - Returns the width of an element (including padding and border)
  • outerHeight() - Returns the height of an element (including padding and border)

Syntax

Return the width:

$(

selector

).width()

Set the width:

$(

selector

).width(

value

)

Set the width using a function:

$(

selector

).width(function

(index,currentwidth)

)

Parameters Description
value Required when setting the width. Specifies the width of the element, in px, em, pt, etc.
The default unit is px.
function*(index,currentwidth)* Optional. Specifies a function that returns the new width of the selected elements.
  • index - Returns the index position of the element in the set.
  • currentwidth - Returns the current width of the selected elements.
  • |

    More examples

    Set the width of an element
    How to set an element's width using different units.

    Increase the width using a function
    How to use a function to increase an element's width.

    Return the width of the document and window elements
    How to return the current width of the document and window elements.

    Display dimensions with related methods
    How to use width(), height(), innerHeight(), innerWidth(), outerWidth(), and outerHeight().


    jQuery HTML/CSS methods jQuery HTML/CSS methods