How to set the padding of an element with JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

Use the padding property in JavaScript to set the padding of an element. You can try to run the following code to add padding of an element with JavaScript −

Example

Live Demo

<!DOCTYPE html>
<html>
   <head>
      <style>
         #box {
            border: 2px solid #FF0000;
            width: 150px;
            height: 70px;
         }
      </style>
   </head>
   
   <body>
      <div id = "box">This is demo text.</div>
      <br><br>
      <button type = "button" onclick = "display()">Add Padding</button>

      <script>
         function display() {
            document.getElementById("box").style.padding = "20px 30px 10px 50px";
         }
      </script>
   </body>
   
</html>
raja
Published on 27-Feb-2018 11:36:14
Advertisements