如何使用 JavaScript 設定元素的頂部填充?


在 JavaScript 中使用 paddingTop 屬性來設定頂部填充。你可以嘗試執行以下程式碼來使用 JavaScript 設定元素的頂部填充 −

示例

<!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()">Top Padding</button>
     
      <script>
         function display() {
            document.getElementById("box").style.paddingTop = "20px";
         }
      </script>
   </body>
</html>

更新於: 23-Jun-2020

491 瀏覽量

開啟你的 職業生涯

完成課程並獲得認證

入門
廣告