Computer >> Máy Tính >  >> Lập trình >> CSS

Làm thế nào để tạo một hình ảnh đáp ứng với CSS?


Sau đây là mã để tạo hình ảnh đáp ứng với 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>

Đầu ra

Đoạn mã trên sẽ tạo ra kết quả sau -

Làm thế nào để tạo một hình ảnh đáp ứng với CSS?

Khi thay đổi kích thước trình duyệt, hình ảnh sẽ giảm tỷ lệ như sau -

Làm thế nào để tạo một hình ảnh đáp ứng với CSS?