jQuery outerWidth()

Introduction

Return the outer width of a <div> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*from  ww  w  . j  av  a  2s .  co  m*/
<script>
$(document).ready(function(){
  $("button").click(function(){
    document.getElementById("demo").innerHTML = 
       "Outer width of div: " + $("div").outerWidth();
  });
});
</script>
</head>
<body>

<p id="demo"></p>

<div style="height:100px;width:300px;padding:10px;margin:3px;background-color:lightblue;">
</div><br>

<button>Display the outer width of div</button>

</body>
</html>

The outerWidth() method returns the outer width of the first matched element.

The returned value from this method includes padding and border.

To include the margin, use outerWidth(true).

$(selector).outerWidth(include_Margin)
Parameter
Optional
Description
include_Margin


Optional.


whether or not to include the margin
false - Default. Does not include the margin
true - Includes the margin



PreviousNext

Related