Javascript DOM CSS Style margin Property set

Introduction

Change all four margins of a <div> element to "25px":

document.getElementById("myDiv").style.margin = "25px";

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {/*from www  .  j av a 2 s  .  c o  m*/
  border: 1px solid #FF0000;
  margin: 2cm 4cm 3cm 2cm;
}
</style>
</head>
<body>

<div id="myDiv">This is a div.</div>
<br>
<button type="button" onclick="myFunction()">Change margin</button>

<script>
function myFunction() {
  document.getElementById("myDiv").style.margin = "25px";
}
</script>

</body>
</html>



PreviousNext

Related