Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> CSS

Cách tạo hình ảnh responsive (tự co giãn) bằng CSS

Dưới đây là mã nguồn để tạo hình ảnh responsive (tự động co giãn theo container) bằng CSS:

Ví dụ

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> img { width: 100%; max-width: 1000px; } </style> </head> <body> <h1>Responsive Images Example</h1> <h2>Resize the window to see responsive image scale up and down</h2> <img src="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"> </body> </html>

Kết quả

Mã trên sẽ cho ra kết quả như sau:

Cách tạo hình ảnh responsive (tự co giãn) bằng CSS

Khi thu nhỏ trình duyệt, ảnh sẽ co lại như sau:

Cách tạo hình ảnh responsive (tự co giãn) bằng CSS