👀 Live Preview
An <img> with three width-based candidates in srcset:
Resize the window. The label on the SVG shows which candidate loaded.

The srcset attribute is a core feature of responsive web design. It lets you list multiple image sources on an <img> or <source> element so the browser can pick the best file for the user’s screen width, layout slot, and pixel density—improving load time and sharpness.
Each entry in srcset is a URL plus a descriptor: 500w (image width in pixels) or 2x (pixel density). Always keep a fallback src for older browsers and as the default candidate.
Multiple URLs.
Width in px.
1x, 2x, 3x.
Pair with w.
On source.
.srcset property.
srcsetThe main purpose of the srcset attribute is to optimize image loading on different devices. Instead of sending one huge image to every phone, tablet, and desktop, you provide several files and let the browser choose the smallest one that still looks sharp.
By supplying multiple sources with width or density descriptors, developers ensure users receive a version suited to their display—better performance on slow networks and sharper images on high-DPI screens.
With w descriptors, add the sizes attribute so the browser knows how wide the image appears. See the HTML sizes attribute guide for slot-size syntax.
Comma-separated list of URL + descriptor pairs on <img>:
<img
src="default.jpg"
alt="Responsive photo"
srcset="small.jpg 500w,
medium.jpg 1000w,
large.jpg 1500w"
sizes="(max-width: 600px) 100vw, 800px">url descriptor separated by commas.w descriptor: intrinsic width of the file (e.g. 500w).x descriptor: pixel density (e.g. 1x, 2x).w and x in the same srcset.src (usually a sensible middle size).<img> and <source> (inside picture).The srcset attribute accepts a comma-separated list of image URLs with descriptors:
w) — photo-500.jpg 500w, photo-1000.jpg 1000w — file width in pixels.x) — logo.png 1x, logo@2x.png 2x — for fixed display size.<!-- Width descriptors + sizes -->
srcset="small.jpg 500w, medium.jpg 1000w, large.jpg 1500w"
<!-- Density descriptors (fixed layout width) -->
srcset="icon.png 1x, icon-2x.png 2x"With 500w, 1000w, and 1500w, the browser compares each file’s width against the rendered size (from sizes and device pixel ratio) and downloads the smallest adequate image. The w value is the actual pixel width of the file, not the CSS width.
| Descriptor | Meaning | Needs sizes? |
|---|---|---|
500w | File is 500px wide | Yes (for layout) |
2x | For 2× pixel density | No |
src | Fallback image URL | Always set |
sizes | Display slot widths | With w |
| JavaScript | img.srcset = "..." | Dynamic updates |
| Element | img, source | Responsive images |
| Element | Supported? | Notes |
|---|---|---|
<img> | Yes | Most common use; pair with src and often sizes |
<source> | Yes | Inside <picture> for art direction or formats |
<video>, <script> | No | Use src instead |
<link> imagesrcset | Related | Preload responsive images (separate attribute) |
srcset vs src vs sizes| Attribute | Role | Example |
|---|---|---|
src | Single fallback URL | src="photo.jpg" |
srcset | List of candidate URLs | small.jpg 500w, large.jpg 1500w |
sizes | Rendered width hints | (max-width: 600px) 100vw, 800px |
| Together | Responsive selection | src + srcset + sizes |
w descriptors with sizes, dynamic JavaScript updates, and x descriptors for Retina displays.
An <img> with three width-based candidates in srcset:
Resize the window. The label on the SVG shows which candidate loaded.
A simple implementation with width descriptors:
<img
src="default.jpg"
alt="Responsive image"
srcset="small.jpg 500w,
medium.jpg 1000w,
large.jpg 1500w"
sizes="(max-width: 600px) 100vw, 800px">In this example, the browser chooses among 500px, 1000px, and 1500px files based on how wide the image is displayed and the screen’s pixel density.
Update candidates when content or layout changes:
<img id="dynamicImage" src="default.jpg" alt="Photo">
<script>
var img = document.getElementById("dynamicImage");
img.srcset = "new-small.jpg 500w, new-medium.jpg 1000w, new-large.jpg 1500w";
img.sizes = "(max-width: 600px) 500px, (max-width: 1200px) 900px, 1400px";
</script>In this script, srcset is replaced with new URLs and descriptors. Updating sizes together keeps width-based selection accurate after layout changes.
For fixed-size images like icons, use x descriptors:
<img
src="logo.png"
alt="Site logo"
srcset="logo.png 1x, logo@2x.png 2x"
width="200"
height="80">Density descriptors match device pixel ratio. Use them when the image always displays at the same CSS size; use w + sizes when width varies with layout.
srcset does not replace alt; describe the image once regardless of which file loads.alt="" when appropriate; srcset works the same.<picture> and ensure critical content remains visible.URLs + w or x.
sizes + viewport.
Smallest sharp match.
Right size delivered.
The srcset attribute is supported in all modern browsers on <img> and <source>.
Chrome, Firefox, Safari, and Edge all support srcset with w and x descriptors.
Bottom line: Use srcset for responsive images; keep src as fallback.
w or x descriptorsw descriptors with a correct sizes attributesrc<picture> when helpfulw and x in one srcsetsizes when using w on fluid layoutsalt because you added srcsetThe srcset attribute is a valuable tool for creating responsive, performance-optimized web pages. By listing multiple image candidates, you let the browser deliver the right file for each device.
Combine srcset with sizes, a fallback src, and meaningful alt text for production-ready responsive images. Your users get faster loads and sharper visuals across every screen size.
srcsetBookmark these before your next responsive image.
Image candidates
PurposeWidth descriptor
wRetina density
xLayout hint
PairDynamic list
Dynamicimg and source (inside picture).500w means the image file is 500 pixels wide. Use with sizes for responsive layouts.2x targets displays with twice the pixel density (Retina). Good for fixed-size logos and icons.element.srcset and update sizes when layout changes.src.Practice srcset with w and x descriptors in the Try It editor.
5 people found this page helpful