HTML srcset Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Media & Images

Introduction

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.

What You’ll Learn

01

Candidates

Multiple URLs.

02

w descriptor

Width in px.

03

x descriptor

1x, 2x, 3x.

04

sizes

Pair with w.

05

picture

On source.

06

JavaScript

.srcset property.

Purpose of srcset

The 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.

💡
srcset lists files; sizes describes layout

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.

📝 Syntax

Comma-separated list of URL + descriptor pairs on <img>:

srcset.html
<img
  src="default.jpg"
  alt="Responsive photo"
  srcset="small.jpg 500w,
           medium.jpg 1000w,
           large.jpg 1500w"
  sizes="(max-width: 600px) 100vw, 800px">

Syntax Rules

  • Each candidate: url descriptor separated by commas.
  • w descriptor: intrinsic width of the file (e.g. 500w).
  • x descriptor: pixel density (e.g. 1x, 2x).
  • Do not mix w and x in the same srcset.
  • Always include fallback src (usually a sensible middle size).
  • Valid on <img> and <source> (inside picture).

💎 Values

The srcset attribute accepts a comma-separated list of image URLs with descriptors:

  • Width (w)photo-500.jpg 500w, photo-1000.jpg 1000w — file width in pixels.
  • Density (x)logo.png 1x, logo@2x.png 2x — for fixed display size.
  • URL only — Without a descriptor (legacy; avoid in new code).
srcset-values.html
<!-- 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"

How It Works

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.

⚡ Quick Reference

DescriptorMeaningNeeds sizes?
500wFile is 500px wideYes (for layout)
2xFor 2× pixel densityNo
srcFallback image URLAlways set
sizesDisplay slot widthsWith w
JavaScriptimg.srcset = "..."Dynamic updates
Elementimg, sourceResponsive images

Applicable Elements

ElementSupported?Notes
<img>YesMost common use; pair with src and often sizes
<source>YesInside <picture> for art direction or formats
<video>, <script>NoUse src instead
<link> imagesrcsetRelatedPreload responsive images (separate attribute)

srcset vs src vs sizes

AttributeRoleExample
srcSingle fallback URLsrc="photo.jpg"
srcsetList of candidate URLssmall.jpg 500w, large.jpg 1500w
sizesRendered width hints(max-width: 600px) 100vw, 800px
TogetherResponsive selectionsrc + srcset + sizes

Examples Gallery

w descriptors with sizes, dynamic JavaScript updates, and x descriptors for Retina displays.

👀 Live Preview

An <img> with three width-based candidates in srcset:

Live srcset demo

Resize the window. The label on the SVG shows which candidate loaded.

Example — Responsive Image

A simple implementation with width descriptors:

srcset.html
<img
  src="default.jpg"
  alt="Responsive image"
  srcset="small.jpg 500w,
           medium.jpg 1000w,
           large.jpg 1500w"
  sizes="(max-width: 600px) 100vw, 800px">
Try It Yourself

How It Works

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.

Dynamic Values with JavaScript

Update candidates when content or layout changes:

dynamic-srcset.html
<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>
Try It Yourself

How It Works

In this script, srcset is replaced with new URLs and descriptors. Updating sizes together keeps width-based selection accurate after layout changes.

Example — Pixel Density (2x)

For fixed-size images like icons, use x descriptors:

srcset-x.html
<img
  src="logo.png"
  alt="Site logo"
  srcset="logo.png 1x, logo@2x.png 2x"
  width="200"
  height="80">
Try It Yourself

How It Works

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.

♿ Accessibility

  • One alt per imgsrcset does not replace alt; describe the image once regardless of which file loads.
  • Do not hide meaning in resolution — All candidates should show the same content; only resolution changes.
  • Set width and height — Reserve space to reduce layout shift while the chosen file loads.
  • Decorative images — Use alt="" when appropriate; srcset works the same.
  • Art direction — When crops differ per breakpoint, use <picture> and ensure critical content remains visible.

🧠 How srcset Works

1

Author lists candidates

URLs + w or x.

srcset
2

Browser reads layout

sizes + viewport.

Context
3

Best file selected

Smallest sharp match.

Selection
=

Fast, sharp image

Right size delivered.

Browser Support

The srcset attribute is supported in all modern browsers on <img> and <source>.

HTML5 · Fully supported

Responsive images everywhere

Chrome, Firefox, Safari, and Edge all support srcset with w and x descriptors.

98% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
srcset attribute 98% supported

Bottom line: Use srcset for responsive images; keep src as fallback.

💡 Best Practices

✅ Do

  • Provide multiple sources with accurate w or x descriptors
  • Pair w descriptors with a correct sizes attribute
  • Keep a sensible fallback src
  • Test on mobile, tablet, desktop, and 2× displays
  • Use modern formats (WebP, AVIF) via <picture> when helpful

❌ Don’t

  • Mix w and x in one srcset
  • Skip sizes when using w on fluid layouts
  • Omit alt because you added srcset
  • Assume the largest file always loads
  • Use one oversized image for all breakpoints

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about srcset

Bookmark these before your next responsive image.

5
Core concepts
📏 02

500w

Width descriptor

w
🔍 03

2x

Retina density

x
📄 04

+ sizes

Layout hint

Pair
⚙️ 05

.srcset JS

Dynamic list

Dynamic

❓ Frequently Asked Questions

It lists multiple image URLs with descriptors so the browser picks the best file for the user’s screen and pixel density.
img 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.
Yes. Set element.srcset and update sizes when layout changes.
Yes in all modern browsers. Always provide fallback src.

Deliver the right image to every device

Practice srcset with w and x descriptors in the Try It editor.

Try width descriptors →

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.

5 people found this page helpful