HTML Snippet

Responsive Image with picture

Difficulty: Easy

Serving the right image at the right size saves bandwidth on phones and avoids blurry assets on retina displays. The `<picture>` element plus `srcset` lets the browser pick the best source for the device. This snippet covers density-based srcset for retina, width-based srcset with sizes for fluid layouts, and art direction with multiple `<source>` entries for different aspect ratios.

Code Snippets
/

Responsive Image with picture

Responsive Image with picture

Serving the right image at the right size saves bandwidth on phones and avoids blurry assets on retina displays. The `<picture>` element plus `srcset` lets the browser pick the best source for the device. This snippet covers density-based srcset for retina, width-based srcset with sizes for fluid layouts, and art direction with multiple `<source>` entries for different aspect ratios.

HTML
Easy
3 snippets
html-elements
html-responsive
html-media
code-template

995 views

5

The 1x / 2x / 3x form tells the browser which file to load at each device pixel ratio. The browser picks the smallest image that meets or exceeds the screen density, so a phone with devicePixelRatio: 2 downloads [email protected] instead of the 3x version. Always include explicit width and height so the browser reserves space before the image loads (this prevents layout shift). The 1x entry is also the src for browsers that do not support srcset, so the fallback is automatic.