# Responsive Images Patterns Complete guide to serving optimal images for different devices and screen sizes. --- ## srcset with Named Variants Best for consistent, predefined sizes. ```html Responsive image ``` **Variants to create**: - `mobile`: width=480, fit=scale-down - `tablet`: width=768, fit=scale-down - `desktop`: width=1920, fit=scale-down --- ## srcset with Flexible Variants Best for dynamic sizing (public images only). ```html Responsive image ``` --- ## Art Direction (Different Crops) Serve different image crops for mobile vs desktop. ```html Art directed image ``` **Variants to create**: - `mobile-square`: width=480, height=480, fit=cover - `desktop-wide`: width=1920, height=1080, fit=cover --- ## High-DPI (Retina) Displays Serve 2x images for high-resolution screens. ```html Retina-ready image ``` --- ## Blur Placeholder (LQIP) Load tiny blurred placeholder first, then swap to full image. ```html Image with LQIP ``` --- ## Lazy Loading Defer loading below-the-fold images. ```html ... Lazy loaded image ``` --- ## URL Transformations (/cdn-cgi/image/) Transform ANY publicly accessible image (not just Cloudflare Images storage). ```html Transformed origin image ``` --- ## Recommended Breakpoints ```javascript const breakpoints = { mobile: 480, // Small phones tablet: 768, // Tablets desktop: 1024, // Laptops wide: 1920, // Desktops ultrawide: 2560 // Large displays }; ``` **sizes attribute**: ```html sizes=" (max-width: 480px) 480px, (max-width: 768px) 768px, (max-width: 1024px) 1024px, (max-width: 1920px) 1920px, 2560px " ``` --- ## Complete Example ```html Hero image Gallery image ``` --- ## Best Practices 1. **Always use format=auto**: Optimal WebP/AVIF delivery 2. **Add loading="lazy"**: Below-the-fold images 3. **Match sizes to CSS layout**: Use `sizes` attribute correctly 4. **Provide descriptive alt text**: Accessibility 5. **Use LQIP for perceived performance**: Better UX 6. **Named variants for private**: Signed URLs compatible 7. **Flexible variants for public**: Dynamic sizing 8. **Limit srcset to 3-5 sizes**: Balance performance vs flexibility --- ## Official Documentation - **Responsive Images (MDN)**: https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images - **Cloudflare Images**: https://developers.cloudflare.com/images/