A wide road-bike photo shows the whole frame. On a narrow screen, a closer crop makes the drop handlebar and fork easier to see. Use <picture> to select the composition, then use srcset and sizes within each composition if you need several resolutions.
Provide both compositions
Save the wide image and portrait crop next to your HTML file to run this example.
Save the wide image as responsive-image-sizes-bike-1200.webp and the portrait as picture-bike-detail-540.webp. Their download URLs use hashes. In a standalone file, include the viewport meta tag so a phone selects the crop for its device width.
<meta name="viewport" content="width=device-width, initial-scale=1">
<picture>
<source media="(width < 600px)" srcset="picture-bike-detail-540.webp"
width="540" height="675">
<img src="responsive-image-sizes-bike-1200.webp" width="1200" height="750"
alt="White road bike with black drop handlebars">
</picture>
picture img {
display: block;
max-width: 100%;
height: auto;
}
The browser checks the source before the fallback <img>. Below 600 CSS pixels the portrait crop matches. At 600 CSS pixels and above, the wide image applies. The selected source can supply dimensions to reserve space for the different aspect ratio. Keep an alternative text on the single <img> that fits both compositions. HTML picture and source elements
Check the actual file
Download the exercise PDF and save its attached ZIP using a PDF viewer that supports attachments. Extract the files together and open picture-mobile-crop.html in a browser. Set the viewport to 599 and 600 CSS pixels. The output names the selected file. Verify that the narrow file shows the handlebar and fork and the wide file shows the whole bike. In your own page, inspect the image’s currentSrc; also check that its naturalWidth is greater than zero. The URL can be present even if the file fails to load.
If both widths should show the same crop, use responsive image sizes to choose a resolution.