HTML Picture Element & Srcset
Responsive images for all devices and screen sizes
🖼️ What are Picture Element & Srcset?
The
<picture>
element and
srcset
attribute help you serve the perfect image for each device and screen size. Think of it as having multiple versions of the same image and letting the browser pick the best one!
<!-- Basic responsive image -->
<img src="image-800.jpg"
srcset="image-400.jpg 400w, image-800.jpg 800w, image-1200.jpg 1200w"
sizes="(max-width: 600px) 400px, 800px"
alt="Responsive image">
Why Responsive Images Matter
Mobile Performance
Smaller images for mobile users save bandwidth and load faster
High-DPI Screens
Sharp images for Retina displays and high-resolution screens
Different Layouts
Images adapt to different container sizes across devices
Better Performance
Faster loading times with optimized image delivery
🔹 Basic Srcset (Resolution Switching)
Use srcset to provide different image sizes for different screen widths:
<img src="image-400.jpg"
srcset="image-400.jpg 400w,
image-800.jpg 800w,
image-1200.jpg 1200w"
sizes="(max-width: 600px) 400px,
(max-width: 900px) 800px,
1200px"
alt="Responsive image">
How it works:
The browser picks the best image based on screen size and pixel density. The
w
descriptor tells the browser each image's width in pixels.
🔹 Understanding Srcset Syntax
Breaking down the srcset attribute:
srcset="image-small.jpg 400w,
image-medium.jpg 800w,
image-large.jpg 1200w"
Syntax explanation:
-
image-small.jpg 400w- Image is 400 pixels wide -
image-medium.jpg 800w- Image is 800 pixels wide -
image-large.jpg 1200w- Image is 1200 pixels wide -
wstands for "width descriptor"
🔹 Understanding Sizes Attribute
The sizes attribute tells the browser how big the image will be displayed:
sizes="(max-width: 600px) 400px,
(max-width: 900px) 800px,
1200px"
Translation:
- On screens 600px or smaller: image will be 400px wide
- On screens 600-900px: image will be 800px wide
- On screens larger than 900px: image will be 1200px wide
🔹 Pixel Density Descriptors
For high-DPI screens like Retina displays:
<img src="image.jpg"
srcset="image.jpg 1x,
image-2x.jpg 2x,
image-3x.jpg 3x"
alt="High-DPI image">
Pixel density explained:
-
1x- Normal screens (standard resolution) -
2x- Retina/high-DPI screens (twice the pixel density) -
3x- Very high-DPI screens (three times the density)
🔹 The Picture Element
Use picture for art direction - completely different images for different screens:
<picture>
<source media="(max-width: 600px)" srcset="mobile-image.jpg">
<source media="(max-width: 1200px)" srcset="tablet-image.jpg">
<img src="desktop-image.jpg" alt="Responsive image">
</picture>
Use case:
Perfect when you want completely different images for different screen sizes, like cropped portraits for mobile and landscape images for desktop.
🔹 Modern Image Formats
Serve modern formats with fallbacks for better compression:
<picture>
<source srcset="image.avif" type="image/avif">
<source srcset="image.webp" type="image/webp">
<img src="image.jpg" alt="Modern format image">
</picture>
How it works:
Browser tries AVIF first (best compression), then WebP (good compression), finally falls back to JPEG (universal support).
🔹 Real-World Examples
🔸 Hero Image (Full Width)
<img src="hero-1200.jpg"
srcset="hero-400.jpg 400w,
hero-800.jpg 800w,
hero-1200.jpg 1200w,
hero-1600.jpg 1600w"
sizes="100vw"
alt="Hero image">
🔸 Content Image (Max 600px)
<img src="content-600.jpg"
srcset="content-300.jpg 300w,
content-600.jpg 600w"
sizes="(max-width: 600px) 100vw, 600px"
alt="Content image">
🔹 CSS Integration
Making responsive images work with CSS:
<style>
.responsive-image {
max-width: 100%;
height: auto;
}
.hero-image {
width: 100%;
height: 50vh;
object-fit: cover;
}
</style>
<img src="image.jpg"
srcset="image-400.jpg 400w, image-800.jpg 800w"
sizes="100vw"
class="responsive-image"
alt="Responsive image">
🔹 Best Practices
✅ Do This:
-
Always include a fallback
srcattribute -
Use width descriptors (
w) for most responsive images - Optimize all image versions for web
- Test on real devices and different screen sizes
- Use modern formats (WebP, AVIF) with fallbacks
❌ Avoid This:
-
Forgetting the
sizesattribute withsrcset - Using only pixel density descriptors for all images
- Not providing fallback images
- Making image files unnecessarily large