Javascript DOM CSS Style left Property set

Introduction

Set the left position of a <div> element:

document.getElementById("myDIV").style.left = "100px";

Click the button to position the DIV element 100 pixels from the left edge:

If the position property is set to "static", the left property has no effect.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {//from  w  w w  . ja  v a  2 s . c o  m
  position: absolute;
  width: 100px;
  height: 100px;
  background-color: coral;
  color: white;
}
</style>
</head>
<body>
<button onclick="myFunction()">Test</button>

<div id="myDIV">
  <h1>myDIV</h1>
</div>

<script>
function myFunction() {
  document.getElementById("myDIV").style.left = "100px";
}
</script>

</body>
</html>



PreviousNext

Related