The srcset attribute is a powerful tool for optimizing images on your website. It allows you to provide multiple versions of an image at different sizes, enabling browsers to choose the most appropriate version based on the user's device and screen size. Here's why it's important:
By using srcset, you ensure that your website delivers the optimal image for each user's device, improving both performance and user experience.
This image uses srcset to serve different sizes based on the viewport width:
<img
srcset="/static/example_425w.webp 425w,
/static/example_567w.webp 567w,
/static/example_756w.webp 756w,
/static/example_1008w.webp 1008w,
/static/example_1344w.webp 1344w,
/static/example_1792w.webp 1792w"
sizes="(max-width: 425px) 425px,
(max-width: 567px) 567px,
(max-width: 756px) 756px,
(max-width: 1008px) 1008px,
(max-width: 1344px) 1344px,
1792px"
src="/static/example.webp"
alt="Responsive image example"
>
Try resizing your browser window to see how the image adapts!
PageSpeed often flags large images because they can significantly slow down page load times. This negatively impacts user experience and SEO, as search engines favor faster-loading pages. Large images consume more bandwidth and take longer to render, especially on mobile devices or slower connections.
<picture> element with <source> for WebP and fallback to original format.
<picture>
<source type="image/webp"
srcset="image-425w.webp 425w, image-567w.webp 567w, image-756w.webp 756w,
image-1008w.webp 1008w, image-1344w.webp 1344w, image-1792w.webp 1792w"
sizes="(max-width: 425px) 425px, (max-width: 567px) 567px, (max-width: 756px) 756px,
(max-width: 1008px) 1008px, (max-width: 1344px) 1344px, 1792px">
<img src="image-original.jpg" alt="Description" loading="lazy" decoding="async">
</picture>
This approach ensures that users receive appropriately sized images for their devices, improving load times and overall performance, which can positively impact SEO rankings.