Loading...

HTML essentialsMedia

Responsive images: srcset, sizes & picture

6 min lesson

Next
  • A responsive image loads a different file depending on the screen size and resolution.
  • srcset lists several image files with their widths, and sizes tells the browser how wide the image will be displayed.
  • Think of it as a wardrobe: you offer several sizes, and the browser picks the one that fits the screen.
  • <picture> lets you give completely different images for different conditions, such as a wide banner and a tall phone image.
  • This builds on Media: you now control which file the browser downloads, not just how it plays.
  • The example uses srcset on one image and a picture element for art direction; you will see the browser choose the right file.
  • Next, SVG, canvas & iframes adds graphics and embedded content.
Keep in mind
  • Use srcset and sizes for the same picture at different sizes.
  • Use picture when you need a different crop or format, not just a smaller file.
  • Always keep a plain src as a fallback for older browsers.
  • Test by resizing the window; check the Network panel to confirm the chosen file.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Responsive images</title>
</head>
<body>
  <!-- The browser picks the best file from srcset based on size -->
  <img
    src="cat-400.jpg"
    srcset="cat-400.jpg 400w, cat-800.jpg 800w, cat-1200.jpg 1200w"
    sizes="(max-width: 600px) 100vw, 50vw"
    alt="A small orange cat on a chair">

  <!-- picture offers completely different images per condition -->
  <picture>
    <source media="(min-width: 800px)" srcset="banner-wide.jpg">
    <img src="banner-narrow.jpg" alt="Mountain banner">
  </picture>
</body>
</html>

What happens: A page showing the cat image, sized to fit the screen, and below it a banner that is wide on large screens and narrow on small screens.

Breaking it down

src

src="cat-400.jpg"

The fallback file. Used by old browsers or when no srcset candidate fits, so never leave it out.

srcset

cat-400.jpg 400w, cat-800.jpg 800w

Lists candidate files with their real widths in w units. The browser picks based on screen size and density.

sizes

sizes="(max-width: 600px) 100vw, 50vw"

Tells the browser how wide the image will be displayed, so it can pick the right file before layout.

picture

<picture>...</picture>

Lets you choose a different image per condition with source media. The img inside is the fallback.

Responsive image tools

ToolUse for
srcsetThe same picture in several sizes
sizesHow wide the picture will display
pictureDifferent pictures for conditions
source mediaA condition like a screen width

A helpful analogy

srcset is like offering the same shirt in small, medium and large; the shop assistant (the browser) picks the size that fits. picture is like choosing a completely different style for different weather.

Nesting rules

  • 1.source must come before the img inside picture.
  • 2.The img inside picture must still have src and alt.

Common mistakes

Using picture when only a size change is needed

Avoid

<picture> for a smaller copy of the same photo

Use instead

srcset with several sizes

srcset lets the browser choose the size. picture is for genuinely different images or crops.

Lying in sizes

Avoid

sizes="100vw" when the image is half width

Use instead

sizes="50vw"

The browser uses sizes to pick a file. A wrong value picks a file that is too small or too large.

Quick tips

  • •Add 1x and 2x descriptors for retina screens: srcset="cat.jpg 1x, [email protected] 2x".
  • •Resize the browser and watch the Network panel; the chosen file changes as the width changes.
Previous
Next