Home HTML Attributes srcset HTML srcset Attribute Definition
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.
Syntax
HTML Syntax <img srcset=" url descriptor, … " …>
Comma-separated candidates. Example: small.jpg 500w, medium.jpg 1000w or icon.png 1x, icon-2x.png 2x.
Values
Attribute Values Two descriptor styles:
Pattern Description Try it … 500w, … 1000wWidth descriptors — pair with sizes Try it… 1x, … 2xDensity descriptors — fixed-size images / icons Try itpicture + sourceArt direction / format switching Try it
Try it
Live Preview Change the sizes hint while the same srcset candidates stay fixed. The label shows which candidate the browser picked (currentSrc).
Code
HTML Examples Short study snippets. Use Try It Yourself for a live editor.
1. Width descriptors 2. Density descriptors 3. picture + source Example 1 — Width Descriptors + sizes Most common pattern for fluid layouts.
<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"> How It Works srcset lists files by intrinsic width (w).sizes describes how wide the image will display.Browser multiplies that slot by DPR, then picks a candidate.src remains the fallback / largest default.Example 2 — Density Descriptors (1x / 2x) Good for fixed-size icons and logos.
<img
src="logo.png"
srcset="logo.png 1x, logo-2x.png 2x"
width="120"
height="60"
alt="Site logo"> How It Works 1x / 2x describe pixel density, not layout width.No sizes needed for density-only lists.src counts as the 1x candidate when supported.Retina screens prefer the 2x file when available.Example 3 — picture with source srcset Art-direction: different crops or formats per media query.
<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> Resize below 600px to prefer the mobile source.
How It Works source can win based on media (or type).srcset on source lists candidates for that branch.img is the required fallback and default branch.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 .
Compatibility
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.
Remember
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 What does the HTML srcset attribute do? srcset lists multiple image URLs with descriptors so the browser can choose the best file for screen size, layout slot, and pixel density.
Which elements use srcset? Primarily img and source (often inside picture). Keep a fallback src on img for older browsers.
What are w and x descriptors? 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.
Do I need sizes with srcset? Yes when using w descriptors so the browser knows how wide the image displays. Density (x) descriptors usually do not need sizes.
Can I mix w and x in one srcset? No. Do not mix width descriptors and density descriptors in the same srcset attribute.
Is srcset the same as src? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion