Example

Set the width of all p elements:

$("button").click(function(){
  $("p").width(200);
});

Try it yourself »

Definition and Usage

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


Return Width

Returns the width of the FIRST matched element.

Syntax

$(selector).width()
Try it yourself »


Set Width

Sets the width of ALL matched elements.

Syntax

$(selector).width(value)
Try it yourself »

Parameter Description
value Required. Specifies the width in px, em, %, etc. Default unit is px


Set Width Using a Function

Using a function to set the width of ALL matched elements.

Syntax

$(selector).width(function(index,oldwidth))
Try it yourself »

Parameter Description
function(index,oldwidth) Specifies a function that returns the new width of selected elements.
  • index - Optional. Receives the index position of the selector
  • oldwidth - Optional. Receives the current width of the selector


Examples

Try it Yourself - Examples

Get the width of the document and window elements
How to use the width() method to get the current width of the document and window elements.

Set the width using em and % values
How to set the width of an element using other length units.