Lesson 8 of 8

Images that fill their boxes

Goal

A fixed ratio without distortion

01

Explanation

Images don't come in a single shape: some are portrait, some landscape and some square. If you leave each one at its original ratio, cards sitting side by side end up with different heights and the row looks ragged.

Two properties solve this. aspect-ratio: 16 / 9 forces the box into that ratio whatever its width, and object-fit: cover fills the box with the image and crops whatever extends past its edges. The trap is writing a fixed height instead. The boxes do match, but the default value of object-fit, fill, stretches the image so the circle turns into an oval, and the fixed height stops fitting the moment the width changes.

That leaves the corners. The card has rounded corners and the image has sharp ones that poke out past the border, so you put overflow: hidden on the card to clip everything outside its bounds.

02

Your task

Make the image in each .card as wide as the card, at a 16:9 ratio, filling its box without distortion, and make the card clip whatever spills past its edges.

Checks

5 checks
  1. Every image is 16:9
  2. The image spans the full width of the card
  3. The image isn't distorted, thanks to object-fit: cover
  4. The ratio holds when the card's width changes
  5. The card clips what spills past its edges
03

Hints

0/3
Track lessons0 of 8 done

Go deeper into the box model, custom properties, positioning, container queries and more, so you understand what's really going on behind the styles you see.

Intermediate level, 8 lessons, 1.5 hours in total.

index.html
Preview
index.html100%
–/5

Press “Run and check” to grade your code.

5 checks listed under “Your task”.

Track lessons0 of 8 done

Go deeper into the box model, custom properties, positioning, container queries and more, so you understand what's really going on behind the styles you see.

Intermediate level, 8 lessons, 1.5 hours in total.