Computer >> Computer tutorials >  >> Programming >> Javascript

How to set the top position of an element with JavaScript?


To set the top position, use the top property in JavaScript. You can try to run the following code to set the top position of a div with JavaScript −

Example

<!DOCTYPE html>
<html>
   <head>
      <style>
         #myID {
            position: absolute;
         }
      </style>
   </head>

   <body>
      <button onclick="display()">Set Top Position</button>
      <div id = "myID">
         This is demo text! This is demo text!
      </div>

      <script>
         function display() {
            document.getElementById("myID").style.top = "100px";
         }
      </script>
   </body>
</html>