# object-fit & aspect-ratio

> Master responsive media by controlling image scaling without distortion and locking container proportions.

Subject: CSS | Lesson 29 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/object-fit-and-aspect-ratio
Previous: [Fluid Type & Responsive Units](https://codewithanimation.in/css/fluid-typography-and-units.md) | Next: [Transitions](https://codewithanimation.in/css/transitions.md)

## What they are

`aspect-ratio` allows you to lock an element's proportions. If you set `aspect-ratio: 16 / 9`, the browser will automatically calculate the height based on the width to maintain that exact rectangle.

`object-fit` dictates how the content of a replaced element (like an `<img>` or `<video>`) should resize to fit its container.

## Why it matters

When building responsive grids (like a gallery of product photos), you usually want all image boxes to be exactly the same size. But user-uploaded photos come in all shapes and sizes.

If you force an image to be `width: 100%` and `height: 100%`, it will squish and stretch to fit the box, looking terrible.

`object-fit: cover` solves this by telling the image to fill the box entirely while maintaining its intrinsic proportions, cropping any overflow.

## How to use them

1. **Lock the box**: Apply `aspect-ratio: 1 / 1` (a perfect square) to the image container.

2. **Fill the box**: Make the image fill the container using `width: 100%; height: 100%`.

3. **Fix the distortion**: Apply `object-fit: cover` to the image to crop it perfectly, or `object-fit: contain` to letterbox it.

## Common mistake: Cumulative Layout Shift (CLS)

Before `aspect-ratio` existed, pages would jump wildly as images loaded because the browser didn't know how tall the image was until it downloaded. Setting an `aspect-ratio` reserves the exact space on the page immediately, eliminating this jarring layout shift.

## Tip: Positioning the crop

If `object-fit: cover` is cropping off the most important part of an image (like a person's face), you can use the `object-position` property to shift the focus. For example, `object-position: top` ensures the top of the image is never cropped.

## Try it: Try it

Toggle the different object-fit values to see how the browser resolves the conflict between the image's original dimensions and its fixed container size.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/object-fit-and-aspect-ratio

## Check yourself

### Question 1

If you have a square container but a tall, rectangular image, what will `object-fit: cover` do?

- A. It will scale the image to fill the square completely, cropping off the top and bottom of the image.
- B. It will shrink the image so the whole thing fits inside the square, leaving empty space on the sides.
- C. It will stretch the image to make it square, making it look distorted.

Correct answer: A

`cover` ensures the container is completely filled. It maintains the image's aspect ratio by scaling it up and cropping whatever overflows.

- Why not B: That is what 'object-fit: contain' does.
- Why not C: That is the default behavior if you force width/height without object-fit.

### Question 2

If an element has `width: 400px` and `aspect-ratio: 4 / 1`, what will its height be?

- A. 100px
- B. 1600px
- C. 400px

Correct answer: A

A 4:1 ratio means the width is 4 times larger than the height. 400 divided by 4 equals 100.

- Why not B: The ratio is 4 (width) to 1 (height). So height is width / 4.
- Why not C: That would be an aspect ratio of 1 / 1.

### Question 3

How does `aspect-ratio` help improve a website's Core Web Vitals score?

- A. It prevents layout shifts by reserving the exact space for an image before it loads.
- B. It compresses the image file size to make it load faster.
- C. It lazy-loads images automatically.

Correct answer: A

By defining the aspect ratio, the browser knows exactly how much height to reserve for the image container immediately, preventing Cumulative Layout Shift (CLS).

- Why not B: CSS cannot compress image files.
- Why not C: Lazy loading requires the 'loading="lazy"' HTML attribute.

## Remember this

- `aspect-ratio: w / h` locks an element's proportions, reserving space before images load to prevent layout shifts.
- Forcing an image's `width` and `height` causes terrible distortion.
- `object-fit: cover` fixes distortion by filling the box and cropping the overflow.
- `object-fit: contain` fixes distortion by shrinking the image to fit entirely, leaving empty letterbox space.
