Image Sizing

Responsive Images: Serving the Right Size to Every Device

How srcset, sizes and the picture element let browsers pick the best image for each screen, saving bandwidth without hurting sharpness.

On this page
  1. The Problem in Plain Terms
  2. Resolution Switching With srcset
  3. The sizes Attribute
  4. Density Descriptors
  5. Art Direction With picture
  6. Choosing Formats
  7. Prevent Layout Shifts
  8. Loading Behavior
  9. Generating the Files
  10. Testing
  11. Frequently Asked Questions

A single large image on a web page looks fine on a desktop monitor and wastes data on a phone. A single small image loads quickly on a phone and looks blurry on a large, high-density display. Responsive images solve this by offering the browser several versions and letting it choose.

This guide explains the HTML tools that make that possible and how to use them without guesswork.

The Problem in Plain Terms

Browsers do not know in advance how big an image should be, and designers do not know which device a visitor will use. Screens differ in width, and they also differ in pixel density. A phone might show a 400 pixel wide image across 800 or 1200 physical pixels.

If you serve one file, you must pick a compromise. Serve a huge file and you slow down every visitor. Serve a small file and sharp screens look soft. Responsive images remove the compromise by describing the options and letting the browser decide.

Resolution Switching With srcset

The srcset attribute lists several files of the same picture at different widths. Each entry gives a URL and a width descriptor ending in w, which is the real pixel width of that file.

<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 600px"
  width="800"
  height="600"
  alt="A hiker on a mountain trail">

The src value is a fallback for old browsers. The srcset list tells newer browsers what exists. The browser then picks one based on the display size, the screen density and sometimes network conditions.

The sizes Attribute

The browser needs to know how wide the image will appear in the layout before it can choose a file. It often has to decide before the CSS has fully loaded, so you tell it with sizes.

The value is a list of media conditions and widths, ending with a default. In the example above, the image fills the full viewport width on screens up to 600 pixels, and is 600 pixels wide otherwise. The vw unit means a percentage of the viewport width.

Getting sizes right matters. If you omit it, the browser assumes the image fills the whole viewport and may download a file larger than necessary. If you write a value that does not match your layout, it may choose a file that is too small.

Density Descriptors

There is a simpler form of srcset that uses density descriptors such as 1x and 2x. It suits images that keep a fixed display size, such as a logo.

<img src="logo.png" srcset="logo.png 1x, logo@2x.png 2x" width="200" height="60" alt="Company logo">

Use width descriptors for fluid layouts, and density descriptors for fixed-size graphics.

Art Direction With picture

Sometimes you want different crops, not just different sizes. A wide landscape photo may need a tighter crop on a narrow screen so the subject stays visible. The picture element handles this.

<picture>
  <source media="(max-width: 600px)" srcset="hero-portrait.jpg">
  <source media="(min-width: 601px)" srcset="hero-wide.jpg">
  <img src="hero-wide.jpg" width="1200" height="600" alt="Workshop table with tools">
</picture>

The browser takes the first source whose condition matches. The img tag inside is required and acts as the fallback and the element that gets styled.

Choosing Formats

The picture element can also offer different file formats. List the more efficient formats first, with a widely supported one last.

<picture>
  <source type="image/avif" srcset="photo.avif">
  <source type="image/webp" srcset="photo.webp">
  <img src="photo.jpg" width="800" height="600" alt="Street market at dusk">
</picture>

Browsers skip any format they cannot decode. This lets you adopt newer formats without breaking older devices.

Prevent Layout Shifts

Always include width and height attributes that match the image's aspect ratio. Modern browsers use them to reserve space before the file arrives, so text does not jump when the image loads. Pair them with CSS such as max-width: 100% and height: auto to keep images flexible.

Loading Behavior

Two more attributes help performance.

  • **loading="lazy"** delays images that are off screen until the visitor scrolls near them. Do not use it on the main image at the top of the page, since that should load as soon as possible.
  • **decoding="async"** allows the browser to decode the image without blocking other work.

Generating the Files

Making many versions by hand is tedious. Build tools, image servers and content management systems can generate a set of widths automatically. A sensible set often spans from a small mobile size up to the largest size the layout can show, with steps that are not too close together. Too many versions waste storage, and too few reduce the benefit.

Keep the originals at the highest quality, and produce each variant from the original, not from another resized copy.

Testing

Open your browser's developer tools, check the network panel, and resize the window or switch to a device mode. Confirm that different files load at different widths and that none is far larger than the space it fills.

Frequently Asked Questions

Do I need srcset for every image?

Not for tiny icons or small fixed graphics. It helps most for large photos and hero images, where the difference between a mobile and desktop file can be significant.

What is the difference between srcset and picture?

The srcset attribute offers different sizes of the same image and lets the browser choose. The picture element lets you control which image or crop appears under specific conditions, and also allows format switching.

Will the browser always pick the smallest file that fits?

Not always. The browser weighs screen density, layout size and sometimes network conditions, and it may choose a larger file than the minimum to keep the image sharp. Treat the choice as the browser's decision and provide sensible options.