Skip to content
AITroveRead. Build. Understand.
Make this comfortable

CSS object-fit and aspect ratio: crop a preview without distorting it

Last updated: 5 Oct 20265 min read
tutorial
BeginnerBy AITrove Editorial

The aspect-ratio property can reserve a box's proportions. object-fit controls how a replaced image or video fits into that box.

How the rule works

A case index displays small receipt scans with inconsistent source dimensions. A fixed ratio and cover fit keep the card grid stable, but the crop can hide a signature or amount. Use the crop only for a navigational thumbnail; the detail page must expose the full evidence. If every pixel matters on the card, contain is safer and may leave letterboxing. Supply width and height attributes in HTML when known, and write alternate text from the image's function. CSS changes the crop, not the bytes transferred or the accessibility text.

css
.receipt-card__thumb { inline-size: 100%; aspect-ratio: 4 / 3; object-fit: cover; }
.receipt-detail__scan { max-inline-size: 100%; block-size: auto; object-fit: contain; }

Cost and verification

Cropping does not reduce download size. Serve an appropriately sized thumbnail asset or responsive source set when the original scan is large. Test cards with portrait, landscape, and unexpectedly small images, and confirm the full record remains reachable. An aspect-ratio placeholder can reduce layout movement while data loads, but it cannot fix a source file with the wrong orientation.

Common Mistakes

  • Do not use cover on evidence when the cropped part may change the decision.
  • Do not treat object-fit as image compression.
  • Do not remove meaningful alternate text because the crop looks clear.

Connected lessons

Property decisions

Further interaction details

Further rendering decisions

css
type-media
web
Storage details