Javascript DOM CSS Style padding Property compare to margin

Introduction

Difference between the margin property and the padding property:

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {//  w  w  w  .j  a v  a 2s. c  o m
  border: 1px solid #FF0000;
}
</style>
</head>
<body>

<div id="myDiv">This is some text.</div>
<br>
<button type="button" onclick="changeMargin()">
Change margin of the div element</button>
<br>
<br>

<div id="myDiv2">This is some text.</div>
<br>
<button type="button" onclick="changePadding()">
Change padding of the div element</button>

<script>
function changeMargin() {
  document.getElementById("myDiv").style.margin = "100px";
}

function changePadding() {
  document.getElementById("myDiv2").style.padding = "100px";
}
</script>

</body>
</html>



PreviousNext

Related