Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

การสร้างแอนิเมชันสำหรับคุณสมบัติ CSS padding-left

ตัวอย่างการสร้างแอนิเมชันเปลี่ยน padding ซ้าย (padding-left) ของ element

ตัวอย่างโค้ด

<!DOCTYPE html>
<html>
<head>
  <style>
    div {
      width: 350px;
      height: 150px;
      outline: 3px solid orange;
      animation: myanim 3s infinite;
    }
    @keyframes myanim {
      50% {
        padding-left: 20px;
      }
    }
  </style>
</head>
<body>
  <h1>CSS padding-left property</h1>
  <div> </div>
</body>
</html>

คำอธิบาย: กล่องมี outline สีส้ม เริ่มต้น padding-left = 0 ที่ 50% จะเพิ่มเป็น 20px เนื้อหาภายในจะถูกผลักไปขวา

ข้อสังเกต: ตัวอย่างใช้ outline แทน border เพราะ outline ไม่ส่งผลต่อขนาดกล่อง (box model) ทำให้เห็นการเปลี่ยนแปลง padding ชัดเจนกว่า

แนวทางที่ดีกว่า: ใช้ transform: translateX(20px) บน child element แทน