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

ใช้ CSS max-width Property ทำ Responsive Image

ใช้ max-width: 100% และ height: auto เพื่อทำให้ Image Responsive ไม่เกินขนาดต้นฉบับ

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        img {
            max-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>

ข้อดี: รูปจะไม่ใหญ่กว่าต้นฉบับ (ไม่เบลอ) แต่จะย่อลงได้เมื่อ Container เล็กกว่า