Responsive Images
Master responsive image techniques with srcset, sizes, picture element, modern formats, and performance optimization for all devices and screen densities.
How do srcset and sizes work for responsive images?
srcset lists the same image at several widths (photo-480.jpg 480w, photo-1080.jpg 1080w) and sizes tells the browser how wide the image will be displayed ((max-width: 600px) 100vw, 50vw). The browser then downloads the smallest file that looks sharp on that screen, which saves a lot of data on phones.
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(max-width: 640px) 100vw, 50vw"
alt="Mountain lake at sunrise"
width="1600" height="1067" loading="lazy">Responsive Images Revolution
Responsive images automatically adapt to different screen sizes, pixel densities, and bandwidth conditions. Modern implementation requires understanding srcset, sizes, picture element, and format optimization for optimal user experience and performance.
Mobile First
Optimized for mobile devices and varying screen sizes
Multi-Device
Perfect display on tablets and varying orientations
Desktop Ready
High-resolution support for desktop displays
Retina Support
Sharp images on high-DPI screens
Srcset & Sizes Attributes
Resolution Switching
Width Descriptors
srcset="image-400.jpg 400w,
image-800.jpg 800w,
image-1200.jpg 1200w"Density Descriptors
srcset="logo-1x.png 1x,
logo-2x.png 2x,
logo-3x.png 3x"Sizes Attribute Examples
Mobile First
sizes="(max-width: 480px) 100vw, (max-width: 768px) 50vw, 33vw"Complex Layout
sizes="(max-width: 480px) calc(100vw - 40px), (max-width: 768px) calc(50vw - 20px), 300px"Picture Element & Art Direction
The picture element provides art direction control, allowing different images for different contexts while the img element handles the actual display and fallback.
Art Direction
- • Different crop for mobile vs desktop
- • Portrait vs landscape orientations
- • Focus on different subjects per device
- • Optimized composition for screen size
Format Fallbacks
- • AVIF for modern browsers
- • WebP for broad support
- • JPEG/PNG for legacy browsers
- • Automatic format selection
Picture Element Structure
<picture>
<source media="(max-width: 480px)" srcset="mobile.jpg">
<source media="(max-width: 768px)" srcset="tablet.jpg">
<img src="desktop.jpg" alt="Responsive image">
</picture>Complete Responsive Images Implementation
Comprehensive responsive images examples covering srcset, sizes, picture element, modern formats, lazy loading, and performance optimization techniques.
Code Editor
<img
src="https://picsum.photos/id/1039/800/450"
srcset="https://picsum.photos/id/1039/400/225 400w,
https://picsum.photos/id/1039/800/450 800w,
https://picsum.photos/id/1039/1600/900 1600w"
sizes="(max-width: 600px) 100vw, 50vw"
alt="Waterfall in a forest" width="800" height="450">
<p id="chosen"></p>Preview
Performance & Optimization
Modern Formats
- AVIF: 50% smaller than JPEG
- WebP: 25-30% smaller than JPEG
- JPEG XL: Next-gen compression
- Progressive JPEG: Faster perceived loading
Loading Strategies
- Lazy loading: Load when needed
- Progressive: Low-quality first
- Preloading: Critical images first
- Intersection Observer: Viewport-based loading
Optimization Tips
- Compression: Balance quality vs size
- CDN delivery: Global edge caching
- Image sprites: Reduce HTTP requests
- Critical path: Above-fold priority
Performance Checklist
Image Optimization
- Use modern formats (AVIF, WebP)
- Implement lazy loading
- Optimize compression settings
- Use appropriate dimensions
Delivery Optimization
- Enable browser caching
- Use CDN for global delivery
- Implement preload hints
- Monitor Core Web Vitals
Responsive Images — step by step
Use srcset for resolution switching
The srcset attribute lets the browser choose the best image based on device pixel ratio or viewport width. Provide multiple sources like image-400.jpg 400w, image-800.jpg 800w and let the browser pick the optimal one.
🎯 Quick Check
What is the purpose of the sizes attribute on an img element?
Your Learning Path
Master responsive images for all device sizes
What You've Learned
- ▶Picture element for responsive images
- ▶Source element with media queries
- ▶Srcset attribute for image resolution
- ▶Image optimization techniques
- ▶Performance best practices
What's Next
- ▶Picture Element - Advanced responsive images
- ▶SVG Graphics - Scalable vector images
- ▶WebP and Modern Formats - Next-gen images
- ▶Lazy Loading - Performance optimization
- ▶CSS Media Queries - Responsive design
Pro Learning Tip
Responsive images are crucial for modern web performance. Test your images on different devices and always provide fallbacks for older browsers that don't support modern features.
Responsive Images: summary
Key points
- Width descriptors (
800w) withsizeshandle different layouts; density descriptors (2x) suit fixed-size images like logos. - Without
sizes, the browser assumes100vwand may download a much larger file than needed. widthandheightattributes still matter: they prevent layout shift.- Frameworks such as Next.js
<Image>generatesrcsetfor you.
Frequently asked questions
›What is the difference between srcset and picture?
srcset offers different sizes of the same image and lets the browser choose. <picture> lets you choose different images or formats, for example a cropped image on mobile, or AVIF with a JPEG fallback.