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

เพิ่ม Transition Effect สำหรับ Width และ Height พร้อมกันด้วย CSS

เพื่อเพิ่ม transition effect ให้ทั้ง width และ height ของอิลิเมนต์

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

<!DOCTYPE html>
<html>
<head>
  <style>
    div {
      width: 150px;
      height: 150px;
      background: blue;
      transition: width 3s;
    }
    div:hover {
      width: 250px;
      height: 250px;
    }
  </style>
</head>
<body>
  <h1>Heading One</h1>
  <p>Hover เมาส์ที่กล่องด้านล่างเพื่อเปลี่ยนความกว้างและความสูง:</p>
  <div></div>
</body>
</html>

หมายเหตุสำคัญ: โค้ดนี้ระบุ transition: width 3s แค่ width เท่านั้น ดังนั้น height จะเปลี่ยนทันที (ไม่มี transition) หากต้องการให้ทั้งคู่มี transition ให้ใช้ transition: width 3s, height 3s; หรือ transition: all 3s;