Javascript DOM CSS Style marginLeft Property get

Introduction

Return the left margin of a <div> element:

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

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {//from   www.  j ava 2s  .co  m
  border: 1px solid #FF0000;
}
</style>
</head>
<body>

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

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

</body>
</html>



PreviousNext

Related