jQuery outerWidth() Method

The jQuery outerWidth() method returns or sets the outer width of the selected element. The value of outer width includes the padding and border of the element you select.

You can also include the margin property with the outer width. Pass a boolean value as a parameter of the method to include the margin

jQuery outerWidth() Method

Syntax of jQuery outerWidth() Method

The syntax of this method is given below:-

Return the width of an element
$(selector).outerWidth()
Sets the width of an element
$(selector).outerWidth(includeMargin)

Description of the Parameters

The description of the parameters is given below.

Parameter Name Description
selector Specify the element class, id, or tag name to select and get or sets the outer width.
includeMargin You have to specify the boolean value to decide whether to include margin or not. If you specify true, it includes the margin with outer width. Also, if you specify false, it will not include the margin with outer width.

jQuery outerWidth() Method Returns the Value of Outer Width

To get or return the outer width of the selected element, you have to use the method as given below. It returns the outer width that includes the padding and the border of the selected element.

The example contains the button and the div element. The div element contains some width, height, padding, and margin.

Example

Test it Live

Output

When you click the button above, you will get the outer width in the alert box. This outer width includes the element width, padding, and border of the selected element.

How to Include Margin with Outer Width

To get the outer width including the margin, pass ‘true’ as the parameter of the method. See the example below contains the same elements as given above.

Example

Test it Live

Output

You have to click the button above to get the outer width including the margin. The output displays in the alert box includes the margin of the selected element. The value of the outer width is different from the previous example value.

You may also like to read

I hope you like this tutorial on jQuery outerWidth() method. If you have any queries regarding the tutorial, please comment below.

References