Reading history

Responsive Images: srcset, sizes and the picture Element

6 min read 0 views

Serve the right image to every device with srcset and sizes, switch to WebP and AVIF with picture, change crops for phones, and stop layout shift with width and height.

Images are usually the heaviest part of a web page. Sending a 2,000-pixel photo to a phone with a 400-pixel screen wastes data and slows the page, which matters a lot on mobile networks. Responsive images let the browser choose the right file for each device. This lesson covers srcset and sizes, the <picture> element, modern formats such as WebP and AVIF, and the attributes that stop layouts from jumping.

In this lesson you will learn

  • Offering several sizes with srcset and width descriptors
  • Telling the browser the display size with sizes
  • Pixel-density descriptors for fixed-size images
  • Format switching and art direction with <picture>
  • Lazy loading and preventing layout shift

You should know the img element and responsive design.

srcset and sizes

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>srcset and sizes</title>
  <style>
    img { max-width: 100%; height: auto; display: block; }
  </style>
</head>
<body>
  <!-- The browser picks the smallest file that is big enough -->
  <img
    src="hills-800.jpg"
    srcset="hills-400.jpg 400w,
            hills-800.jpg 800w,
            hills-1600.jpg 1600w"
    sizes="(max-width: 700px) 100vw, 700px"
    width="800" height="400"
    alt="Green hills under a yellow sun"
    loading="lazy"
    decoding="async">

  <!-- For a fixed-size image, describe pixel density instead -->
  <img src="logo.png" srcset="logo.png 1x, logo@2x.png 2x"
       width="120" height="40" alt="Core Tech ED logo">
</body>
</html>
  • srcset lists the available files with their real widths: hills-400.jpg 400w means that file is 400 pixels wide.
  • sizes tells the browser how wide the image will be shown, before the CSS has loaded. Here: the full viewport width on screens up to 700px, otherwise 700px.
  • The browser combines the two with the screen’s pixel density. On a 360px phone with a 2× screen, it needs about 720 pixels, so it picks hills-800.jpg. On a laptop it may pick 800 or 1600. You describe the options; the browser makes the choice.
  • src is the fallback for very old browsers and must still be present.
  • For an image that is always the same size, such as a logo, density descriptors are simpler: logo.png 1x, logo@2x.png 2x.
  • width and height let the browser reserve the right space before the file arrives, so text does not jump down when it loads. CSS height: auto keeps the image’s shape when it is resized.
  • loading="lazy" waits until the image is near the screen; decoding="async" lets the page carry on while it is decoded.

The picture Element

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>picture element</title>
</head>
<body>
  <!-- 1. Format switching: the first supported type wins -->
  <picture>
    <source type="image/avif" srcset="market.avif">
    <source type="image/webp" srcset="market.webp">
    <img src="market.jpg" alt="Vegetable stalls at a busy market" width="900" height="600">
  </picture>

  <!-- 2. Art direction: a different crop for narrow screens -->
  <picture>
    <source media="(max-width: 600px)" srcset="bridge-tall.jpg">
    <img src="bridge-wide.jpg" alt="The Padma Bridge at sunset" width="1200" height="500">
  </picture>
</body>
</html>
  • Inside <picture>, the browser reads the <source> elements in order and uses the first one that matches, then shows it through the <img>.
  • Format switching: type="image/avif" and type="image/webp" offer smaller modern formats; a browser that understands neither falls back to the JPEG.
  • Art direction: media="(max-width: 600px)" serves a different, taller crop on narrow screens, so the bridge does not become a thin strip.
  • The <img> is required. It supplies the alt text, the size and the fallback, and CSS styles it, not the picture element.

Image Formats

FormatBest forNotes
JPEGPhotosSupported everywhere
PNGGraphics with sharp edges or transparencyLarge for photos
WebPPhotos and graphicsUsually 25–35% smaller than JPEG at similar quality
AVIFPhotosOften smaller still; slower to create
SVGLogos, icons, diagramsVector: sharp at any size

A Practical Checklist

  1. Resize photos to the largest size they will be shown at; never upload straight from the camera.
  2. Offer two or three widths in srcset and write an honest sizes.
  3. Serve WebP or AVIF through <picture> where you can.
  4. Always set width, height and meaningful alt text.
  5. Lazy-load images below the fold, but not the main image at the top of the page.

Common Mistakes

  1. Leaving out sizes with width descriptors. The browser then assumes the image fills the whole screen and may download a file far too big.
  2. Lazy-loading the hero image, which delays the most important picture on the page.
  3. Styling <picture> instead of the <img> inside it.
  4. Empty or missing alt on images that carry information.

Practice Exercises

Exercise 1. An image is shown at half the screen width on screens wider than 900px and full width otherwise. Write its sizes attribute.

Show answer

sizes="(min-width: 901px) 50vw, 100vw"

Exercise 2. Write a <picture> that offers team.webp and falls back to team.jpg.

Show answer
<picture>
  <source type="image/webp" srcset="team.webp">
  <img src="team.jpg" alt="Our school debate team" width="800" height="500">
</picture>

Frequently Asked Questions

How do I make the different sizes of an image?

Image editors can export several sizes, and command-line tools or online converters can do it in bulk. Many content management systems, including WordPress, create several sizes automatically and write the srcset for you.

What is Cumulative Layout Shift?

A measure of how much a page’s content jumps around while loading. Images without width and height are a common cause. It is one of Google’s Core Web Vitals, which affect search ranking.

Summary

  • srcset offers files; sizes says how wide the image will be shown.
  • <picture> switches formats and crops; it always needs an <img>.
  • WebP and AVIF are much smaller than JPEG.
  • Set width, height and alt; lazy-load only images below the fold.

Next: CSS Scroll Snap and Smooth Scrolling.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *