jQuery outerWidth()

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery outerWidth()

jQuery outerWidth() method is used to return the outer width of the first matched

element with padding and border.

The jQuery outerWidth () method works for both visible and hidden elements.

In the above image, you can see that jQuery outerWidth() method includes border and padding both.

Syntax:

  1. $(selector).outerWidth(includeMargin)  

Parameters of jQuery outerWidth() method

Parameter Description
includeMargin It is an optional parameter. It is a Boolean value which specifies whether to include the margin or not.
  • False:It is a default value. It specifies that not to include margin.
  • True:It specifies that include the margin.

Example of jQuery outerWidth() method

Let's take an example to demonstrate the effect of jQuery outerWidth() method.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $("button").click(function(){  
  8.         alert("Outer width of div is: " + $("div").outerWidth());  
  9.     });  
  10. });  
  11. </script>  
  12. </head>  
  13. <body>  
  14. <div style="height:100px;width:500px;padding:10px;margin:3px;border:1px solid blue;background-color:lightpink;"></div><br>  
  15. <button>Click here to get the outer width of the div</button>  
  16. </body>  
  17. </html>  
Test it Now

jQuery outerWidth() example 2

  1. <!DOCTYPE html>  
  2. <html>  
  3.    <head>  
  4.       <title>The jQuery Example</title>  
  5.       <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>  
  6.           
  7.       <script type="text/javascript" language="javascript">  
  8.          $(document).ready(function() {  
  9.             $("div").click(function () {  
  10.                var color = $(this).css("background-color");  
  11.                var width = $(this).outerWidth( true );  
  12.                $("#result").html("Outer Width is <span>" + width + "</span>.");  
  13.                $("#result").css({'color': color, 'background-color':'white'});  
  14.             });  
  15.          });  
  16.       </script>  
  17. <style>  
  18.          #div1{ margin:10px;padding:10px; border:2px solid #666; width:60px;}  
  19.          #div2 { margin:15px;padding:15px; border:4px solid #666; width:60px;}  
  20.          #div3 { margin:20px;padding:20px; border:6px solid #666; width:60px;}  
  21.          #div4 { margin:25px;padding:25px; border:8px solid #666; width:60px;}  
  22. </style>  
  23. </head>  
  24. <body>  
  25. <p>Click on any square:</p>  
  26. <span id="result"> </span>  
  27. <div id="div1" style="background-color:orange;"></div>  
  28. <div id="div2" style="background-color:green;"></div>  
  29. <div id="div3" style="background-color:brown;"></div>  
  30. <div id="div4" style="background-color:violet;"></div>  
  31. </body>  
  32. </html>  

Test it Now