Course contentsShow
← Web Development
Lesson 426 of 2,9827. Responsive DesignFree lesson

Viewport-Based Image Sizing

Using vw units in sizes to make images scale fluidly with viewport width.

Viewport-Based Image Sizing

You'll learn how to make images scale proportionally with the browser window using viewport units in the sizes attribute.


What Is Viewport-Based Image Sizing?

Remember how you learned about viewport units (vw, vh) earlier? You can use those same units in the sizes attribute to tell the browser how much of the viewport width an image will occupy. This creates truly fluid images that automatically adjust to any screen size without fixed breakpoints.

Instead of saying "this image is 500px at medium screens," you say "this image is always 80% of the viewport width." The browser does the math in real-time as the window resizes.

How It Works

When you write sizes="80vw", you're telling the browser: "This image will take up 80% of the viewport width, no matter what the screen size is." The browser then uses this information with your srcset to pick the most appropriate image file.

Example:

<img 
  srcset="small.jpg 400w, medium.jpg 800w, large.jpg 1200w"
  sizes="80vw"
  src="medium.jpg"
  alt="Responsive image">

Here, if the viewport is 1000px wide, the browser knows the image will display at 800px (80% of 1000px) and chooses the best match from your srcset.

Why This Matters

This approach eliminates guesswork. The image scales smoothly at every width, not just at your predefined breakpoints. It's perfect for hero images, full-width backgrounds, or any layout where the image size is tied directly to the viewport.

Key Takeaway: Using vw units in sizes creates truly fluid images that scale proportionally with the viewport, letting the browser calculate the perfect source for any screen size automatically.