Responsive Images with Picture.
The <picture> element allows you to provide different versions of an image for different screen sizes or device resolutions, significantly improving performance and art direction.
The Essentials
The <picture> element is a container for one or more <source> tags and one <img> tag.
The <source> tag uses 'srcset' to point to different image files.
The 'media' attribute allows you to use media queries to choose the right image.
The browser picks the FIRST <source> that matches the media query.
The <img> tag is required at the end as a fallback for older browsers.
Responsive images save bandwidth by not loading huge files on small mobile screens.
Professional Insights
Art Direction vs. Resolution Switching
Device Pixel Ratio (DPR)
WebP and Modern Formats
The Importance of the Fallback <img>
Critical Pitfalls
Forgetting to include the <img> tag inside the <picture> element.
Putting the 'alt' attribute on the <source> tag instead of the <img> tag.
Not ordering <source> tags correctly—the browser stops at the first match.
Using huge 4K images on mobile because you didn't provide a smaller source.