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

JavascriptWeb DevelopmentFront End Scripts

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

Live Demo

<!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>
raja
Published on 27-Feb-2018 16:26:46
Advertisements