Learning Tips | Published Nov 17, 2026 | 8 min read

Responsive Images in HTML: Choose the Right Source

Learn HTML srcset and sizes for resolution switching, picture for art direction, and practical image loading and alt-text tips.

Diagram of sizes describing an image layout slot, srcset supplying 480, 800 and 1280 pixel candidates, and picture offering alternate crop or format with img fallback

Responsive images let the browser choose a suitable image file for the space and display it has, instead of sending the same oversized file to every device. HTML provides srcset and sizes for resolution switching, and picture for art direction or format alternatives. This guide shows how to choose between them and avoid common loading mistakes.

Two different image problems

Resolution switching means the picture content and crop stay the same, but the browser can choose among different pixel widths. Art direction means the composition changes for a layout, such as a tighter portrait crop on a narrow screen. Use srcset and sizes for the first problem; use <picture> with media-specific sources for the second.

Offer width candidates with srcset

For a flexible image whose rendered width changes with the layout, provide several files of the same image at known intrinsic widths. The width descriptor must match the actual pixel width of each file:

<img
  src="/images/workshop-800.jpg"
  srcset="
    /images/workshop-480.jpg 480w,
    /images/workshop-800.jpg 800w,
    /images/workshop-1280.jpg 1280w"
  sizes="(max-width: 640px) 100vw, 720px"
  width="1280"
  height="853"
  alt="Students working together at a laptop">

The sizes value describes the expected rendered slot width: here it is about the viewport width up to 640 pixels, and 720 pixels on wider screens. The browser combines that hint with the candidate widths and display density to choose a resource. Tune sizes to your actual CSS layout; if the hint claims an image fills the viewport when it really occupies a narrow column, the browser may download a larger file than necessary.

The width and height attributes describe the image's intrinsic dimensions and let the browser reserve the correct aspect ratio before it loads. Keep their ratio consistent with the files, and use CSS such as max-width: 100%; height: auto; to make the image fit its container.

Use picture when the crop or format changes

For a different crop on small screens, provide art-directed sources in a <picture>. The browser checks the source conditions and uses the first compatible match; the nested <img> remains the fallback and supplies alternative text:

<picture>
  <source
    media="(max-width: 600px)"
    srcset="/images/workshop-portrait.jpg">
  <source
    type="image/avif"
    srcset="/images/workshop-wide.avif">
  <img
    src="/images/workshop-wide.jpg"
    width="1280"
    height="720"
    alt="Students collaborating on a project">
</picture>

Use media when the composition should change with the layout, and type when offering a preferred format with a fallback. Keep the sources visually equivalent when changing format, and make sure the final <img> works if none of the optional sources match. For alternative resolutions of one crop, use srcset on the <img> rather than creating a long list of nearly identical <source> elements.

Choose meaningful alternative text

For informative images, write concise alt text that communicates the relevant content or purpose. Do not repeat nearby captions word for word. For a decorative image that adds no information, use an empty alt="" so assistive technology can skip it. The alternative text belongs on the fallback <img>, not on the <picture> wrapper.

Load images without slowing the page

Use loading="lazy" for images that start below the fold, where delaying the request can avoid fetching unseen content. Avoid lazy-loading the main above-the-fold image; it may be the page's largest contentful element and should be discoverable early. For a likely hero image, fetchpriority="high" can be a useful hint, but use it sparingly and measure the result.

Responsive markup does not replace image optimization. Export files close to the needed dimensions, choose a suitable format, compress them, and avoid serving a 3000-pixel source into a small card. Keep file URLs valid and update all referenced variants together when replacing an image set.

Common mistakes

  • Using 480w for a file that is actually 600 pixels wide. Descriptors report intrinsic width, not a desired CSS width.
  • Adding width candidates but omitting sizes for a flexible layout. The browser then has a poor estimate of the rendered slot.
  • Using <picture> for every responsive image, even when the crop is identical and only resolution differs.
  • Changing the crop without checking that the alt text still describes the selected image's purpose.
  • Lazy-loading every image, including the visible hero image, or assigning high fetch priority to many images.

Practical checklist

  • Use srcset with width descriptors for the same image at multiple sizes.
  • Write an accurate sizes hint that matches the page layout.
  • Use <picture> for art direction or a format fallback, and keep an <img> with a valid src and alt text.
  • Set intrinsic dimensions to reserve layout space and prevent image shift.
  • Lazy-load below-the-fold images; keep the main visible image eager and prioritize it only when appropriate.
  • Inspect the chosen resource in browser developer tools at real viewport widths and device pixel ratios.

The browser makes the final choice from the candidates and hints. Your job is to offer files that exist, describe their real dimensions, and describe the layout slot accurately. Start with a small set of well-sized variants, then confirm the browser selects them as expected.

Further reading

Want a structured Java learning path?

Explore Jaipur Engineers course options to connect Java fundamentals, debugging, backend development and project practice in a guided learning plan.

Explore the Java program   Ask for course guidance