jQuery outerWidth() method returns the outer width of the selected element. Border and padding both are included in this method.
Syntax:
$(selector).outerWidth(includeMargin)
includeMargin: This parameter accepts Boolean value to specify whether the margin is to include or not.
- False: Not to include the margin.
- True: To include the margin.
Example1:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ alert("Outer width of the div is: " + $("div").outerWidth()); }); }); </script> </head> <body> <div style="height:100px;width:500px;padding:5px;margin:3px;border:1px solid blue;background-color:lightpink;"></div><br> <button>Width</button> </body> </html>
Example2:
<!DOCTYPE html> <html> <head> <style> div { width: 60px; padding: 10px; height: 100px; float: left; margin: 5px; background: orange; cursor: pointer; } .mod { background: turquoise; cursor: default; } </style> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <div>Thick</div> <div>Thin</div> <div>Thinner</div> <div>Thinnest</div> <script> var modwidth = 100; $( "div" ).one( "click", function() { $( this ).outerWidth( modwidth ).addClass( "mod" ); modwidth -= 10; }); </script> </body> </html>