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 −


<!DOCTYPE html>
         #myID {
            position: absolute;

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

         function display() {
            document.getElementById("myID") = "100px";
Published on 27-Feb-2018 06:21:46