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

How to set the left margin of an element with JavaScript?


Use the marginLeft property in JavaScript, to set the left margin. You can try to run the following code to set the left margin of an element with JavaScript −

Example

<!DOCTYPE html>
<html>
   <body>
      <button type="button" onclick="display()">Add left margin</button>
      <p id="myID">This is demo text.</p>
      <script>
         function display() {
            document.getElementById("myID").style.marginLeft = "80px";
         }
      </script>
   </body>
</html>