HTML srcset Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The srcset attribute lists multiple image candidates so the browser can pick the best file for the user’s layout width and pixel density. Use it on <img> and <source> (often inside <picture>).

Each entry is a URL plus a descriptor: 500w (file width) or 2x (density). Keep a fallback src. With w descriptors, also set sizes.

Baseline · Widely available.

Not deprecated. Core responsive-images feature across modern browsers.

Do not mix w and x.

MDN: it is incorrect to mix width descriptors and pixel-density descriptors in the same srcset.

HTML Syntax

syntax.html
<img srcset=" url descriptor, …" …>

Comma-separated candidates. Example: small.jpg 500w, medium.jpg 1000w or icon.png 1x, icon-2x.png 2x.

Attribute Values

Two descriptor styles:

PatternDescriptionTry it
… 500w, … 1000wWidth descriptors — pair with sizesTry it
… 1x, … 2xDensity descriptors — fixed-size images / iconsTry it
picture + sourceArt direction / format switchingTry it

Live Preview

Change the sizes hint while the same srcset candidates stay fixed. The label shows which candidate the browser picked (currentSrc).

Demo candidates: SVG strips labeled 500w / 1000w / 1500w. DPR also affects the pick.

Image
srcset live demo strip

HTML Examples

Short study snippets. Use Try It Yourself for a live editor.

Example 1 — Width Descriptors + sizes

Most common pattern for fluid layouts.

HTML
<img
  src="hero-1500.jpg"
  srcset="hero-500.jpg 500w, hero-1000.jpg 1000w, hero-1500.jpg 1500w"
  sizes="(max-width: 600px) 100vw, 800px"
  width="1500"
  height="800"
  alt="Hero banner">
Try It Yourself

How It Works

  1. srcset lists files by intrinsic width (w).
  2. sizes describes how wide the image will display.
  3. Browser multiplies that slot by DPR, then picks a candidate.
  4. src remains the fallback / largest default.

Example 2 — Density Descriptors (1x / 2x)

Good for fixed-size icons and logos.

HTML
<img
  src="logo.png"
  srcset="logo.png 1x, logo-2x.png 2x"
  width="120"
  height="60"
  alt="Site logo">
Try It Yourself

How It Works

  1. 1x / 2x describe pixel density, not layout width.
  2. No sizes needed for density-only lists.
  3. src counts as the 1x candidate when supported.
  4. Retina screens prefer the 2x file when available.

Example 3 — picture with source srcset

Art-direction: different crops or formats per media query.

HTML
<picture>
  <source media="(max-width: 600px)"
          srcset="hero-mobile.jpg 600w, hero-mobile-2x.jpg 1200w"
          sizes="100vw">
  <img src="hero-desktop.jpg"
       srcset="hero-desktop.jpg 1200w, hero-desktop-2x.jpg 2400w"
       sizes="(max-width: 1200px) 100vw, 1200px"
       width="1200"
       height="600"
       alt="Product hero">
</picture>
Try It Yourself

How It Works

  1. source can win based on media (or type).
  2. srcset on source lists candidates for that branch.
  3. img is the required fallback and default branch.
  4. Use picture when crops/formats change — not only for resolution.

Edge Cases & Pitfalls

Wrong descriptors or missing sizes waste bandwidth or look soft.

mix

Never mix w and x

One srcset uses either width descriptors or density descriptors — not both.

sizes

w needs sizes

Without sizes, the default is 100vw, which may over-fetch if the image is narrower.

truth

w must match real file width

The number before w is the image’s intrinsic pixel width, not the CSS display width.

dpr

DPR multiplies the slot

A 400px slot on a 2× screen often needs an ~800w file. That is expected.

fallback

Always keep src + alt

Older browsers and failed candidates still need a usable src and meaningful alt.

Browser Support

srcset is an HTML attribute for responsive image candidates on img and source.

✓ Baseline · Widely available

srcset

Widely available across modern browsers for both width and density descriptors.

96% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer No support
Not supported
srcset Widely supported

Bottom line: Safe for modern sites. Keep src as a fallback and pair w descriptors with accurate sizes.

Key Takeaways

  • Role: candidate list for the best image file.
  • w + sizes: fluid layouts; x: fixed icons.
  • Rule: never mix w and x in one list.
  • Fallback: always provide src and alt.

One line: srcset lists files — sizes says how wide they look — the browser chooses.

Frequently Asked Questions

srcset lists multiple image URLs with descriptors so the browser can choose the best file for screen size, layout slot, and pixel density.
Primarily img and source (often inside picture). Keep a fallback src on img for older browsers.
w is the intrinsic width of the file in pixels and works with sizes. x is pixel density such as 1x or 2x for Retina displays.
Yes when using w descriptors so the browser knows how wide the image displays. Density (x) descriptors usually do not need sizes.
No. Do not mix width descriptors and density descriptors in the same srcset attribute.
No. src is a single URL (or fallback). srcset is a candidate list the browser selects from.

Pair with sizes

Learn how layout-width hints drive candidate selection.

sizes tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful