Responsive Images

Images That Fit the Screen

Example

The simplest responsive image: it never grows wider than its container and keeps its aspect ratio:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>

<img src="https://picsum.photos/id/1016/800/300" alt="Canyon at sunset" style="max-width:100%; height:auto;">

</body>
</html>

Result

srcset and sizes

srcset offers the same image in several widths; the browser downloads the smallest one that looks sharp on the current screen. Phones download small files, large screens get detail.

<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 50vw"
  alt="Team photo">

The picture Element

<picture> chooses between different images or formats. The browser takes the first <source> that matches, with <img> as the fallback:

<picture>
  <source media="(max-width: 600px)" srcset="banner-mobile.jpg">
  <source type="image/avif" srcset="banner.avif">
  <img src="banner.jpg" alt="Summer sale banner">
</picture>