← All Exercises

Keep Card Images Proportional with aspect-ratio

intermediatecss~15min

Background

aspect-ratio removes the padding hack from modern layouts, but a supports fallback keeps older browsers stable.

Problem

Style a card image so the media box always keeps a 16:9 ratio, the image fills and crops without distortion, and browsers without aspect-ratio support still reserve the same height.

Input

A card image with an unknown intrinsic aspect ratio.

Output

CSS for a fixed-ratio, cropped media box plus a fallback.

Sample tests

Sample 1

Input: Portrait source image inside the card
Expected: the media box stays 16:9 and object-fit=cover crops the image without distortion

Sample 2

Input: Browser without aspect-ratio support
Expected: the @supports not rule uses padding-block-start 56.25% to reserve the same height

Your solution

Learning objective

Reserve proportional media space with aspect-ratio and crop replaced content without distortion.