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

ใช้ CSS width Property ทำ Responsive Image

ตั้ง width: 100% และ height: auto เพื่อทำให้ Image Responsive

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        img {
            width: 100%;
            height: auto;
        }
    </style>
</head>
<body>
    <p>To check the effect, you need to resize the browser.</p>
    <img src="https://www.tutorialspoint.com/python/images/python-data-science.jpg" alt="Python Data Science" width="400" height="300">
</body>
</html>

ต่างจาก max-width: width: 100% จะบังคับให้รูปขยายเต็ม Container เสมอ (อาจเบลอถ้ารูปเล็กกว่า Container) ส่วน max-width: 100% จะไม่ให้ใหญ่กว่าต้นฉบับ