Javascript DOM CSS Style left Property with negative value

Introduction

Using negative values - Set the left position of a <div> element:

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

Click the button to position the DIV element -30 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   www  . j a  v  a 2s  .c om*/
  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 = "-30px";
}
</script>

</body>
</html>



PreviousNext

Related