Code has been added to clipboard!

Understand the Meaning of jQuery .outerWidth() and Learn to Use It

Reading time 2 min
Published Jan 19, 2018
Updated Oct 1, 2019

jQuery outerWidth: Main Tips

  • jQuery .outerWidth() gets the outer width of the first element of matched elements in pixels.
  • You can also use jQuery .outerWidth() to set a new width for every specified element.
  • To ensure the results are correct, only use this method on visible elements.
  • jQuery height and width methods are also explained in this tutorial.

Meaning and Usage of .outerWidth()

The .outerWidth() jQuery method is used to set or return the outer width of the selected elements. It includes both borders and padding. You can make it include the margin as well.

In the example below, clicking a button triggers an alert box. It displays the current outer width of the element. Close the alert to change it to 100:

Example
$("button").click(() => {
    alert($("p").outerWidth());
});
$("button").click(() => {
    $("p").outerWidth(100);
});

Note: the .outerWidth() jQuery method does not work on window and document objects.

To return the width, you need to use this syntax:

$(selector).outerWidth(margin);

Note: if the element is empty, function returns undefined.

The optional margin parameter defines whether the .outerWidth() jQuery will include margin. It has a boolean value (false by default).

To set the width, specify the new width in a string, number, or a function returning the needed value:

$(selector).outerWidth(newWidth);

Note: if you only define the new width in a number (no unit specified), jQuery takes it as a number of pixels.

Learn jQuery
Introduction
Selectors
Events
Event Methods
Download
Callback
Chaining
Get and Set
Add Element
Remove Element
Traverse
Parent
Children
Sibling Traverse
Filtering
jQuery AJAX
Effects
Show and Hide
Fade Effect
Slide Effect
Animate Effect
Manipulate CSS
Width and Height
.addClass()
.after()
.animate()
.append()
.appendTo()
.attr()
.before()
.bind() (deprecated)
.blur()
.change()
.click()
.clone()
.css()
.dblclick()
.delay()
.delegate() and .undelegate() (deprecated)
.detach()
.empty()
.end()
.error() (deprecated)
.fadeIn()
.fadeOut()
.fadeTo()
.fadeToggle()
.finish()
.focus()
.focusIn()
.focusOut()
.hasClass()
.height()
.hide()
.hover()
.html()
.innerHeight()
.innerWidth()
.insertAfter()
.insertBefore()
.keypress()
.keyup()
.live() and .die() (deprecated)
.load() (deprecated)
.mousedown()
.mouseenter()
.mouseleave()
.mousemove()
.mouseout()
.mouseover()
.mouseup()
.off()
.offset()
.offsetParent()
.on()
.one()
.outerHeight()
.outerWidth()
.position()
.prepend()
.prependTo()
.prop()
.proxy()
.queue()
.ready()
.remove()
.removeAttr()
.removeClass()
.removeProp()
.replaceAll()
.replaceWith()
.resize()
.scroll()
.scrollLeft()
.scrollTop()
.show()
.slideDown()
.slideToggle()
.slideUp()
.stop()
.submit()
.text()
.toggle()
.toggleClass()
.trigger()
.triggerHandler()
.unbind() (deprecated)
.unload() (deprecated)
.unwrap()
.val()
.width()
.wrap()
Event Properties
event.currentTarget
event.preventDefault()
event.relatedTarget
event.stopImmediatePropagation()
event.stopPropagation()
event.target
event.timeStamp
event.type
event.which
jQuery .find()
jQuery .keydown()
jQuery.noConflict()
pageY and pageX