Skip to main content
Website Performance 5 min read · 1,028 words

Responsive Images Explained: srcset, sizes and the Picture Element

Learn how to serve the right image to every device using srcset, sizes and the picture element, with clear examples and common mistakes to avoid.

N

By Naeem

Published

Website Performance

A single image on a website might be viewed on a small phone, a large desktop monitor and a high-density laptop screen, sometimes on a slow mobile connection. Sending the same large file to every visitor wastes data, slows down pages and hurts the experience. Responsive images solve this by letting the browser choose the best file for each situation. This guide explains the tools HTML gives you and how to use them with confidence.

The Problem

Imagine a photograph that is 2400 pixels wide. On a phone displaying it at 400 CSS pixels wide, even a high-density screen only needs about 800 to 1200 real pixels. Downloading all 2400 pixels means several times more data than needed. Conversely, if you send only a small image to a large screen, it looks blurry. Responsive images avoid both problems.

Understanding Screen Density

Modern screens often have more physical pixels than CSS pixels. A device pixel ratio of 2 means that each CSS pixel is drawn with a two-by-two block of physical pixels. To look sharp on such a screen, an image displayed at 400 CSS pixels wide should ideally have about 800 real pixels.

The Basic Attributes

Start with the standard img element, always with a meaningful alt text, and width and height attributes that reflect the image's intrinsic size, so that the browser reserves space and prevents layout shifts.

srcset with width descriptors

The srcset attribute lists several versions of the same image, each followed by its actual width with a "w" unit:

<img src="photo-800.jpg" srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w" sizes="(max-width: 600px) 100vw, 600px" alt="Description of the photo" width="1600" height="900">

The browser knows the width of each file but not how large you plan to display the image, so you tell it with sizes.

The sizes attribute

The sizes attribute describes how wide the image will be displayed at different viewport widths. In the example above, on viewports up to 600 pixels wide, the image fills the entire width (100vw). On larger screens, it is displayed at 600 pixels. The browser combines this with the device pixel ratio and picks the most suitable file from the srcset list. Writing accurate sizes values matters, because the browser makes its decision before layout is complete.

srcset with density descriptors

For fixed-size images such as logos, you can describe density instead:

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

Art Direction With the Picture Element

Sometimes you do not want the same image at different sizes. You might want a wide crop on desktop and a tighter, portrait crop on mobile so that the subject remains visible. The picture element handles this:

<picture>
  <source media="(max-width: 600px)" srcset="hero-mobile.jpg">
  <source media="(min-width: 601px)" srcset="hero-desktop.jpg">
  <img src="hero-desktop.jpg" alt="Description of the hero image" width="1600" height="700">
</picture>

The browser uses the first source whose media condition matches, and falls back to the img element if none do or if picture is not supported. The img element is required, and it carries the alt text and dimensions.

Serving Modern Formats With Fallbacks

The picture element also lets you offer newer, smaller formats while keeping a safe fallback:

<picture>
  <source type="image/avif" srcset="photo.avif">
  <source type="image/webp" srcset="photo.webp">
  <img src="photo.jpg" alt="Description of the photo" width="1200" height="800">
</picture>

Browsers choose the first format they support. This gives modern visitors the smallest file while older browsers receive the JPG.

Lazy Loading and Priority

Add loading="lazy" to images that appear below the fold, so the browser downloads them only when needed. Do not lazy-load the main image visible at the top of the page, because that delays the Largest Contentful Paint. For that image, consider fetchpriority="high". Adding decoding="async" can also help the browser avoid blocking other work while it decodes images.

How Many Sizes Should You Create?

You do not need dozens. For a content image that can span a full-width layout, three to five widths usually cover most needs, for example 400, 800, 1200 and 1600 pixels. Avoid excessive variants, since each one is a file you must create and store. Generate them from the highest-quality original, never by enlarging a small image.

A Practical Workflow

  1. Decide the maximum size at which the image will ever be displayed.
  2. Export several widths from your original.
  3. Convert them to WebP or AVIF, and keep a JPG or PNG fallback if needed.
  4. Compress each file with an appropriate quality setting.
  5. Write the srcset and sizes attributes, along with width and height.
  6. Test using the browser's developer tools network panel, which shows which file was actually downloaded at different window sizes.

Common Mistakes

  • Using srcset without sizes, which makes the browser assume the image is the full viewport width and often choose a file that is too large.
  • Forgetting width and height, which leads to layout shifts.
  • Lazy-loading the hero image.
  • Providing sizes values that do not match the real layout in your CSS.
  • Using the picture element when srcset would be enough. Use picture for art direction or format switching only.
  • Leaving out alt text, which hurts accessibility.

Create the Versions You Need

To produce WebP, AVIF, JPG or PNG copies of your images, use our free Image Format Converter, which converts directly in your browser so your files never leave your device.

Final Thoughts

Responsive images mean sending the right pixels to the right screen. Use srcset and sizes for resolution switching, picture for art direction and format fallbacks, and always set dimensions and alt text. The result is faster pages, lower data usage and sharper pictures for everyone.

Try It Free · 100% In-Browser

Image Format Converter

Batch convert images between JPG, PNG, WebP, and AVIF (where browser-supported) with quality and transparency background controls.

Launch Tool →
N

Written by

Naeem

Naeem writes practical, no-fluff guides on developer workflows, web performance, privacy-first utilities, digital finance math, and focused productivity.

Keep exploring practical tutorials by Naeem.

All Guides →