Javascript DOM CSS Style margin Property get

Introduction

Return the margins of a <div> element:

alert(document.getElementById("myDiv").style.margin);

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {/*from   w w  w.j av a 2 s .  c  om*/
  border: 1px solid #FF0000;
}
</style>
</head>
<body>

<div id="myDiv" style="margin:2cm 4cm 3cm 5cm;">This is a div.</div>
<br>
<button type="button" onclick="myFunction()">Return margin</button>

<p id="demo"></p>

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

</body>
</html>



PreviousNext

Related