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

JavascriptObject Oriented ProgrammingFront End Technology

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>
raja
Published on 27-Feb-2018 10:51:46
Advertisements