Course contentsShow
← Web Development
Lesson 1231 of 2,98222. Responsive Images and MediaFree lesson

Cropping for Different Aspect Ratios

Serve landscape images on desktop and portrait crops on mobile using picture element art direction.

Cropping for Different Aspect Ratios

What you'll learn: How to serve landscape images on desktop and portrait-cropped versions on mobile using the <picture> element's art direction capabilities.

The Problem: One Crop Doesn't Fit All

Imagine you have a beautiful landscape photo of a mountain range. On desktop, it looks stunning in its wide 16:9 format. But on a narrow mobile screen, that same landscape crop becomes tiny and hard to see—the mountains shrink to dots.

The solution isn't just resizing; it's re-cropping. You need a portrait-oriented crop (maybe 4:5 or 9:16) for mobile that focuses on the most important part of the scene, and your original landscape crop for wider screens.

How Picture Element Solves This

The <picture> element with <source> tags lets you specify different images (not just different resolutions) for different viewport sizes:

<picture>
  <source media="(min-width: 768px)" 
          srcset="mountains-landscape.jpg">
  <source media="(max-width: 767px)" 
          srcset="mountains-portrait.jpg">
  <img src="mountains-landscape.jpg" 
       alt="Snow-capped mountain range at sunset">
</picture>

On desktop (≥768px), the browser loads mountains-landscape.jpg. On mobile, it loads mountains-portrait.jpg—a completely different crop optimized for vertical viewing.

Why This Matters

This is art direction: you're making creative decisions about what the viewer sees at each screen size. A hero image might show a full office scene on desktop but crop to just the person's face on mobile. Product photos might shift from showing context to showing detail.

Key Takeaway: Use the <picture> element with different <source> images (not just different resolutions) to serve appropriately cropped versions—landscape for wide screens, portrait for narrow screens—ensuring your images look intentional at every size.