HTML Responsive Images
In this page:
<img src="default.jpg" srcset="small.jpg 480w, large.jpg 1080w" sizes="(max-width: 600px) 480px, 1080px" alt="Description">
The srcset Attribute
srcset gives the browser a menu of image options at different sizes. The browser picks the best one based on the device screen size and resolution.
- Always include a fallback src for browsers that do not support srcset.
- Accepted values:
- url Nw — image width descriptor, e.g. photo-800.jpg 800w
- url Nx — pixel density descriptor, e.g. [email protected] 2x
- Entries are comma-separated
Example: The srcset Attribute
<img src="photo.jpg" srcset="photo-480.jpg 480w, photo-800.jpg 800w" alt="Photo">
The sizes Attribute
The sizes attribute tells the browser how wide the image will be displayed at different viewport sizes. This helps the browser pick the right srcset option before downloading anything.
- Match your sizes values to your CSS breakpoints for best results.
- Accepted values:
- (media-condition) length — e.g. (max-width: 600px) 100vw
- Final entry is the default length, e.g. 50vw
- Lengths use px, vw, em, calc()
Example: The sizes Attribute
<img src="photo.jpg" srcset="photo-480.jpg 480w, photo-800.jpg 800w" sizes="(max-width: 600px) 480px, 800px" alt="Photo">
The picture Element for Art Direction
Sometimes you do not just want a smaller version of the same image on mobile — you want a completely different crop or composition. The picture element lets you specify entirely different images for different screen sizes.
- Use picture when your image needs different cropping for mobile vs desktop not just different sizes.
- Accepted attributes:
- source media — media query
- source srcset — image URL(s)
- source type — MIME type
- img src / alt — required fallback
Example: The picture Element for Art Direction
<picture>
<source media="(max-width: 600px)" srcset="photo-square.jpg">
<img src="photo-wide.jpg" alt="Photo">
</picture>
Lazy Loading Images
The loading="lazy" attribute tells the browser to defer loading an image until it's about to scroll into view, which speeds up initial page load by not downloading images the visitor may never scroll down to see.
- Accepted values:
- loading — lazy | eager (default)
- decoding — async | sync | auto
Example: Lazy Loading Images
<img src="photo.jpg" alt="Photo" loading="lazy">
Setting width and height to Prevent Layout Shift
Even with responsive images, explicitly setting width and height attributes (or an aspect-ratio in CSS) reserves the correct amount of space on the page before the image finishes downloading, preventing the surrounding content from jumping around as it loads.
- Accepted attributes:
- width — integer pixels
- height — integer pixels
Example: Setting width and height to Prevent Layout Shift
<img src="photo.jpg" width="800" height="600" alt="Photo">
- Forgetting the fallback src attribute when using srcset.
- Omitting the img fallback inside a picture element.
- Using srcset without sizes causing the browser to assume 100vw width.
- srcset provides multiple image size options for the browser to choose from.
- Width descriptors (400w) tell the browser the actual pixel width of each option.
- The sizes attribute tells the browser how wide the image will display.
- The picture element allows completely different images for different screen sizes.
- Always include a fallback src or img for older browsers.
srcset and sizes are supported in all modern browsers. The picture element is supported in Chrome, Firefox, Safari, and Edge. IE11 does not support either.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- HTML Keyboard Shortcuts
- HTML Browser Support
- HTML Character Sets
- HTML Doctypes
- HTML Audio/Video Reference
- HTML Meta Tags
- HTML ARIA Roles
- HTML Input Validation
- HTML Iframe Security
- HTML Responsive Images
- HTML Web Fonts
- HTML Icon Fonts
- HTML Progress & Meter
- HTML Output Element
- HTML Datalist
- HTML Image Maps
- HTML Browser DevTools
- HTML Validation
- HTML Quiz
- HTML Interview Prep