Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
Frontend System Design Roadmap
  1. 1.HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)
  2. 2.TCP and TLS Handshakes: Connection Setup Costs
  3. 3.Browser Network Stack and Connection Pooling
  4. 4.GET vs POST and Idempotency in HTTP
  5. 5.The Critical Rendering Path (CRP)
  6. 6.DOM, CSSOM and the Render Tree
  7. 7.Layout, Paint and Compositing
  8. 8.Reflows and Repaints Explained
  9. 9.Event Loop: Call Stack, Task and Microtask Queues
  10. 10.Async JavaScript: Promises and async/await
  11. 11.Web Workers: Multi-threading in the Browser
  12. 12.Browser Storage: Cookies, Web Storage, IndexedDB
  13. 13.Client-Side Storage: Limits and Security Risks
  14. 14.Choosing the Right Client-Side Storage
  15. 15.HTTP Caching: Cache-Control, ETag, Last-Modified
  16. 16.Browser-Level Caching Mechanisms
  17. 17.Cache Invalidation for Frontend Apps
  18. 18.Structuring a Large Single-Page App
  19. 19.Component-Based Architecture Principles
  20. 20.Props, State and Component Lifecycle
  21. 21.Module Bundling Basics
  22. 22.Core Web Vitals: LCP, INP and CLS
  23. 23.FCP and Time to Interactive (TTI) Explained
  24. 24.Measuring Performance: Lighthouse and WebPageTest
  25. 25.Asset Compression and Minification Basics
  26. 26.What Is a CDN and How Does It Work?
  27. 27.CDN Benefits: Latency, Availability, Offload
  28. 28.CDN Cache Behavior and Invalidation
  29. 29.Global State vs Local Component State
  30. 30.State Patterns: Redux, Zustand and Context
  31. 31.Data Fetching: React Query and SWR
  32. 32.Immutable Data in Frontend State
  33. 33.SSR vs CSR: Rendering Trade-offs
  34. 34.Hydration and Time to First Byte (TTFB)
  35. 35.Static Site Generation (SSG) for Speed and SEO
  36. 36.Incremental Static Regeneration (ISR)
  37. 37.Micro-Frontends: Core Principles
  38. 38.Micro-Frontend Integration Strategies
  39. 39.Communication Between Micro-Frontends
  40. 40.Micro-Frontends: Pros and Cons for Large Teams
  41. 41.Real-Time: Polling, SSE and WebSockets
  42. 42.Real-Time Use Cases: Chat, Alerts, Live Data
  43. 43.Designing a Real-Time Data Flow
  44. 44.Scaling WebSocket Connections
  45. 45.Responsive Images: srcset, sizes and picture
  46. 46.Modern Image Formats: WebP, AVIF, JPEG XL
  47. 47.Lazy Loading Images and Videos
  48. 48.Video Streaming: HLS, DASH, Adaptive Bitrate
  49. 49.REST and GraphQL for Frontend Engineers
  50. 50.Batching, Throttling and Debouncing API Calls
  51. 51.Handling API Errors in the Frontend
  52. 52.Authentication Flows: OAuth and JWT
  53. 53.Global Error Boundaries in React
  54. 54.Error Reporting: Sentry and LogRocket
  55. 55.Graceful Degradation and Fallback UIs
  56. 56.Real User Monitoring (RUM)
  57. 57.CORS: Cross-Origin Resource Sharing
  58. 58.Preventing XSS (Cross-Site Scripting)
  59. 59.CSRF Protection for Frontend Apps
  60. 60.Content Security Policy (CSP)
  61. 61.Storing Auth Tokens: Cookies vs localStorage
  62. 62.PWA Manifest and Installability
  63. 63.Service Workers: Lifecycle and Caching Strategies
  64. 64.Offline Support and Background Sync
  65. 65.Push Notifications for Web Apps
  66. 66.Webpack vs Vite vs Parcel
  67. 67.Code Splitting, Tree Shaking, Asset Optimization
  68. 68.Module Resolution and Hot Module Replacement
  69. 69.Monorepo Tooling: Nx and Lerna
  70. 70.Atomic Design for Component Libraries
  71. 71.Theming and Customization in Design Systems
  72. 72.Accessibility in Component Libraries
  73. 73.Component Library Docs, Testing and Versioning
  74. 74.Publishing and Consuming a Component Library
  75. 75.Full-Text Search: Levenshtein and Fuzzy Matching
  76. 76.Indexing Large Datasets in the Browser
  77. 77.Search Performance: Debouncing and Web Workers
  78. 78.Integrating Backend Search Services
  79. 79.Choosing a Chart Library: D3, Chart.js, Recharts
  80. 80.Rendering Large Datasets: Canvas vs SVG
  81. 81.Interactive, Responsive Data Visualizations
  82. 82.Data Fetching and Aggregation for Dashboards
  83. 83.A/B Test Design and Rollout Strategies
  84. 84.Client-Side vs Server-Side A/B Testing
  85. 85.Feature Flags and Remote Configuration
  86. 86.Experiment Data Collection and Analytics
Frontend System Design Roadmap
45

Frontend System Design · Lesson 45 of 86 · 4 min

Responsive Images: srcset, sizes and picture

In an interview, this topic separates developers who just write markup from those who engineer performant user experiences.

How to handle resolution switching with srcset

The simplest way to provide responsive images is with the srcset attribute on an <img> tag. It lets the browser choose the best image from a list you provide, based on the user's screen density and the image's display size.

You define sources using either a pixel density descriptor (x) or a width descriptor (w).

  • 2x descriptor: srcset="image-1x.jpg 1x, image-2x.jpg 2x". Use this for fixed-size images, like icons or logos, that need to be sharp on high-DPI (Retina) displays. It's simple but inflexible for fluid layouts.
  • w descriptor: srcset="image-400w.jpg 400w, image-800w.jpg 800w". This is what you'll use for most images in a responsive layout. You provide the intrinsic width of each image file in pixels.

The w descriptor is more powerful because it decouples the image choice from the device's pixel ratio. Instead, the browser can make a smarter decision based on the actual space the image will occupy.

Why sizes is the critical part

When you use w descriptors in srcset, you must also provide the sizes attribute. It's not optional for getting this right.

The sizes attribute tells the browser how wide the image will be at different viewport widths. You write it as a list of media conditions paired with slot widths.

<img
  src="images/product-default.jpg"
  alt="A product shot"
  srcset="
    images/product-400.jpg   400w,
    images/product-800.jpg   800w,
    images/product-1200.jpg 1200w,
    images/product-1600.jpg 1600w
  "
  sizes="(max-width: 600px) 100vw, 50vw"
/>

Here’s how the browser reads that sizes attribute:

  1. Is the viewport 600px wide or less? If yes, the image will occupy 100vw (100% of the viewport width).
  2. Otherwise (for any viewport wider than 600px), the image will occupy 50vw.

The browser then uses this information to pick the best image from srcset. For example, on a 1200px wide screen, the image slot is 50vw (600px). On a 2x DPI screen, it needs a 1200px source image, so it will likely download product-1200.jpg.

The trap: Omitting sizes when using w descriptors. If you forget it, the browser defaults to sizes="100vw". If your image is in a 50% width column on a large screen, the browser will still assume it's full-width and download a much larger image than necessary, wasting bandwidth.

When to reach for <picture>

Use the <picture> element when srcset alone isn't enough. It gives you more explicit control. It has two primary use cases: art direction and format switching.

Art direction is when you want to serve a different image, not just a smaller version of the same one. For example, a wide landscape shot on desktop might be a tight, portrait crop on mobile to keep the subject visible.

Format switching is for serving modern image formats like AVIF or WebP to browsers that support them, with a fallback to JPEG for older browsers. This is a huge performance win, as modern formats offer much better compression.

The <picture> tag wraps multiple <source> elements and a final <img> fallback. The browser evaluates each <source> in order and uses the first one that matches.

In an interview, frame this as a trade-off. The <picture> element is more verbose and complex than a simple <img> tag. You use it when the performance gains from format switching or the UX benefits of art direction justify the extra markup.

<picture>
  <!-- 
    Serve AVIF to supporting browsers. This is the most modern format.
    The 'type' attribute lets the browser quickly check for support.
  -->
  <source type="image/avif" srcset="photo.avif" />
  
  <!-- If not, try WebP. -->
  <source type="image/webp" srcset="photo.webp" />
  
  <!-- 
    The 'img' tag is the mandatory fallback. It's also what's used by
    browsers that don't support 'picture' at all.
  -->
  <img 
    src="photo.jpg" 
    alt="A colorful abstract image"
    loading="lazy"
    decoding="async"
  />
</picture>

How the browser decides which image to load

Understanding the browser's logic is key to debugging and explaining your design choices.

For an <img> with srcset and sizes, the process is an algorithm:

  1. Check the viewport width.
  2. Evaluate the sizes attribute to find the first matching media condition.
  3. Use the corresponding slot width (e.g., 50vw, 300px) to calculate the image's display width in CSS pixels.
  4. Multiply this by the device pixel ratio to get the desired image resolution in physical pixels.
  5. Look at the w descriptors in srcset and download the image that is closest to, but not smaller than, this desired resolution.

This allows the browser to download an image that is just right—not wastefully large, but sharp enough for the screen.

For <picture>, the logic is simpler: it's a switch statement. The browser goes through the <source> elements from top to bottom. The very first one whose media or type attribute matches the current environment is chosen, and its srcset is used. All subsequent <source> elements are ignored. If none match, the fallback <img> is used.

Try it

Take a simple HTML page with a few static <img> tags. Convert one of them to be responsive. Create three versions of the image (e.g., 400px, 800px, 1200px wide). Write the srcset and a sizes attribute that assumes the image is full-width on mobile but in a two-column layout on desktop. Open your browser's dev tools, go to the network tab, and resize the viewport to see which image gets downloaded at different sizes.

Next: Learn about the file formats themselves in "Modern Image Formats: WebP, AVIF, JPEG XL".

Lock it in — apply this lesson under interview conditions.

Try a free mock interviewPractice system design problems
Previous

Scaling WebSocket Connections

Next

Modern Image Formats: WebP, AVIF, JPEG XL