Javascript DOM CSS Style top Property set negative value

Introduction

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

document.getElementById("myDiv").style.top = "-10px";

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {/* ww w.j  a v  a2  s .co m*/
  border: 1px solid #FF0000;
  position: relative;
}
</style>
</head>
<body>

<div id="myDiv">This is a div.</div>
<br>

<button type="button" onclick="myFunction()">Set top position of div to -10 px</button>

<script>
function myFunction() {
  document.getElementById("myDiv").style.top = "-10px";
}
</script>

</body>
</html>



PreviousNext

Related