jQuery outerWidth() excludes margin

Introduction

outerWidth() - returns the outer width of an element including padding and border.

outerWidth(true) - returns the outer width of an element including padding, border and margin.

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  w ww .j  av a 2 s. co  m*/
<script>
$(document).ready(function(){
  $("button").click(function(){
    let txt = "";
    txt += "outerWidth() of div: " + $("div").outerWidth() + "</br>";
    txt += "outerWidth(true) of div: " + $("div").outerWidth(true) + "</br>";
    $("div").html(txt);
  });
});
</script>
</head>
<body>

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

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

</body>
</html>



PreviousNext

Related