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

ตั้งค่าความสูงและกว้างของภาพเป็นเปอร์เซ็นต์ด้วย CSS


ใช้หน่วย % สำหรับ width และ height

กำหนด width: 50% และ height: 50% เพื่อทำให้ภาพปรับขนาดตามพาเรนต์

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
  <style>
    img {
      height: 50%;
      width: 50%;
    }
  </style>
</head>
<body>
  <p>Sized image in %</p>
  <img src="https://www.tutorialspoint.com/videotutorials/assets/videos/courses/155/images/course_155_image.png" alt="CSS" width="200" height="200">
</body>
</html>