BF50 Black Friday · 50% off all plans
from $5/mo · Auto-applied ---:--:-- Claim 50% off →
Skip to content

Search Seonio

Type a keyword, domain, or paste a URL

Start free with $1 to see full results. Powered by Seonio
Web Development

Responsive images: srcset, sizes and picture

Responsive images give the browser several files of one image, so it can pick the smallest that still looks sharp. HTML does it with srcset and picture.

Monthly searches · responsive images
2.4k
Keyword research data
responsive images
Responsive images: srcset, sizes and picture
Click the image to view it full size

A responsive image is one image published as several files, with markup that lets the browser choose between them. A phone with a narrow screen downloads a 480-pixel-wide file, a large high-density display gets a 1,600-pixel one, and both look sharp. You list the files and say how wide the image will appear; the browser does the choosing.

Plain CSS can't do this on its own. max-width: 100% makes a big image fit a small screen, but the phone still downloads every pixel. And a script that swaps files after the page loads is too late: browsers start downloading images from the HTML before they've read your CSS and JavaScript.

Three problems, three tools

You want to Use
Serve the same picture at different file sizes for different layout widths srcset with width (w) descriptors, plus sizes
Serve sharper files to high-density screens for an image shown at a fixed size srcset with density (x) descriptors
Show a different crop on small screens, or offer newer formats with a fallback the <picture> element

All three are supported in modern browsers. A browser that doesn't understand them loads the file in src, so always keep src.

Different widths: srcset and sizes

This is the common case: one photo that fills a column, and the column's width depends on the screen.

<img
  srcset="fairy-480w.jpg 480w, fairy-800w.jpg 800w, fairy-1600w.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 800px"
  src="fairy-800w.jpg"
  width="800" height="533"
  alt="A child dressed as a fairy">

srcset lists each file with its real width in pixels, written with a w. sizes tells the browser how wide the image will be displayed: here, the full viewport width on screens up to 600 pixels, and 800 pixels otherwise. The browser takes the first condition in sizes that's true, works out the display width, factors in the screen's pixel density, and picks the best-fitting file.

A few rules trip people up:

  • Order matters in sizes. The browser ignores everything after the first match, so put the most specific conditions first and a plain width last.
  • No percentages. Use pixels, em, vw or a calc(), not 50%.
  • Leaving out sizes means 100vw. The browser then assumes the image is as wide as the screen and may download a far bigger file than a sidebar image needs.

How much does it save? In MDN's own example, the 800-pixel file is 128 KB and the 480-pixel one 63 KB. Multiply that by every image on a page and every phone visit.

Same size, sharper screens: x descriptors

For an image always shown at one size, such as a 320-pixel logo, give density versions instead and skip sizes:

<img srcset="logo-320.png, logo-640.png 2x" src="logo-640.png" alt="Example Ltd">

A standard screen gets the first file (1x is implied), a screen with two device pixels per CSS pixel gets the second. Use one kind of descriptor per srcset: either w values with sizes, or x values without, never both in one list.

Different crops and formats: picture

When the small-screen version should show a different picture, not just a smaller one, use <picture>. This is called art direction: a wide landscape on a desktop, a close-up portrait crop on a phone.

<picture>
  <source media="(max-width: 799px)" srcset="team-portrait.jpg">
  <source media="(min-width: 800px)" srcset="team-wide.jpg">
  <img src="team-wide.jpg" alt="Our team outside the workshop">
</picture>

The browser uses the first <source> whose condition is true. The <img> at the end is required, with src and alt: without it, nothing shows at all. Use media on <source> only for art direction, and don't also put media conditions in a sizes attribute there.

<picture> also handles formats. Each <source> can name a type, and the browser takes the first one it supports:

<picture>
  <source type="image/avif" srcset="hero.avif">
  <source type="image/webp" srcset="hero.webp">
  <img src="hero.jpg" alt="Red bicycle against a white wall" width="1200" height="800">
</picture>

See the picture tag and WebP for more.

Keep the page stable and the main image fast

Set width and height. With both attributes on the <img>, the browser knows the aspect ratio before the file arrives and reserves the space, so the text doesn't jump when the image loads. Use the dimensions of the largest file in srcset. This protects your Cumulative Layout Shift score.

Lazy-load images below the fold, never the main one. loading="lazy" helps images far down the page. On the image most likely to be your Largest Contentful Paint, it only adds delay. Give that one fetchpriority="high" instead. See lazy loading.

Let lazy images size themselves. For lazy-loaded images, sizes="auto" tells the browser to use the width the image actually gets in the layout, so you don't have to repeat your CSS breakpoints. It's only valid together with loading="lazy", and you can list normal sizes after it for browsers that don't support it:

<img loading="lazy" sizes="auto, (max-width: 600px) 100vw, 400px"
     srcset="card-400w.jpg 400w, card-800w.jpg 800w"
     src="card-400w.jpg" width="800" height="600" alt="Walnut desk, front view">

What Google needs from responsive images

Google finds images in the src attribute of <img>, including an <img> inside <picture>. It doesn't index images set as CSS backgrounds. Because some crawlers don't understand srcset or <picture>, Google asks you to always provide a fallback URL in src. Google Search Central

The rest is ordinary image SEO: a descriptive alt attribute, a sensible file name and the image near the text it illustrates.

How to check which file loaded

Open your browser's developer tools, go to the Network panel, filter by images and disable the cache. Then resize the window or switch on device mode and reload. The file name tells you which candidate the browser chose. If a narrow window still loads the largest file, check the order in sizes and remember that desktop browsers won't shrink below a minimum window width.

SEO wiki

Related terms

More definitions from the same corner of SEO and digital marketing.

Seonio

Put it to work on your own site

Keyword research, traffic, backlinks and clustering in one place. Start with a free $1 to test, no card required.