W3Schools.com

jQuery CSS width() Method

jQuery CSS Methods jQuery CSS Methods

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.


jQuery CSS Methods jQuery CSS Methods


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$3.98 Unlimited Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Best Website Templates Top CSS Templates
CREATE HTML Websites
EASY WEBSITE BUILDER
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE