aspect-ratio, object-fit and object-position for Media and Boxes
Keeping proportions in CSS: aspect-ratio for any box, which dimension it computes, content overflow and min-height, auto ratios and image width and height attributes that prevent layout shift, and object-fit and object-position for images and video.
kvi5hnuCode Sprout team member8 min readOn this page
Many boxes have a natural shape: video players are 16, avatars are square, product photos might be 4. CSS has two tools for shapes. aspect-ratio gives any box a fixed proportion, and object-fit decides how an image or video fills a box whose shape differs from its own.
aspect-ratio
aspect-ratio sets the ratio of width to height, as a <ratio> (article 29):
.video { aspect-ratio: 16 / 9; } /* full width, height computed: 9/16 of it */
.avatar { width: 4rem; aspect-ratio: 1; }
.card-image { aspect-ratio: 4 / 3; width: 100%; }
The ratio only takes effect when at least one dimension is automatic. The browser sizes the other dimension, then computes the automatic one from the ratio:
| Declared | Result with aspect-ratio: 16 / 9 |
|---|---|
| nothing (a block in a 400px container) | width 400px (fills), height 225px |
width: 200px | height 112.5px |
height: 90px | width 160px: for a block box, even the width is computed from the ratio |
width: 200px; height: 50px | the ratio is ignored |
The ratio applies to the box that box-sizing (article 36) chooses: with border-box, the outer border box has the exact ratio, padding included. Since the universal border-box rule is common, that's usually what you get.
Before aspect-ratio, the trick was padding-top: 56.25% on a wrapper, using the fact that vertical padding percentages refer to the width (article 39). You'll still see it in old code, but there's no reason to write it now.
When the content doesn't fit
A ratio box with too much content grows taller than the ratio, because block boxes keep an automatic minimum height that fits their content. That's usually what you want for text: nothing is cut off. To keep the exact ratio instead, either let it overflow or make it scroll:
.tile { aspect-ratio: 1; min-height: 0; overflow: auto; } /* stays square, scrolls if needed */
Making the box a scroll container (overflow: hidden, auto or scroll) also removes the automatic minimum; overflow: clip doesn't.

The measurements match the table above, and the right panel shows the automatic minimum at work: without min-height: 0, content wins over the ratio.
Images and auto ratios
Images and videos have a natural aspect ratio from their file. With aspect-ratio: auto (the initial value), they keep it when you change one dimension:
img { max-width: 100%; height: auto; } /* width shrinks, height follows the natural ratio */
The problem is that the browser only learns the ratio when the image file arrives. Until then, the image has no height, and when it loads, everything below it jumps down: a layout shift, which is annoying for users and counts against Core Web Vitals scores.
The fix is to put the image's dimensions in the HTML:
<img src="harbor.jpg" alt="The harbor at dawn" width="1600" height="900">
Browsers turn width and height attributes into aspect-ratio: auto 1600 / 900. auto plus a ratio means "use the natural ratio once the image loads, and this ratio until then", so the space is reserved from the start, while CSS like width: 100%; height: auto still scales the image. Always add both attributes to images and videos.
For non-replaced elements, auto 4 / 3 uses the ratio for the content box, whatever box-sizing says.
object-fit
When an image's box doesn't have the same shape as the image, object-fit decides how the picture is drawn inside the box. It works on replaced elements: <img>, <video>, <canvas>, <iframe> content and <object>.

Watch the red frame: it survives on all four sides only with fill, contain and scale-down. The middle row is the case the name hints at: scale-down never enlarges a picture, so a small image stays sharp at its natural size, while contain and cover blow it up and blur it. With cover, object-position picks which strip of the picture you keep.
body { font: 600 0.8rem system-ui, sans-serif; display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.75rem; margin: 1rem; }
figure { margin: 0; }
img { width: 100%; height: 80px; background: repeating-linear-gradient(45deg, #ddd 0 6px, #fff 6px 12px); border: 2px solid #8a3b12; display: block; box-sizing: border-box; }
figcaption { margin-top: 0.25rem; }
| Value | The picture | Aspect ratio kept |
|---|---|---|
fill (initial) | stretched to the box | no |
contain | scaled to fit inside the box completely, leaving empty space | yes |
cover | scaled to cover the whole box, cropping what doesn't fit | yes |
none | drawn at its natural size, cropped or with gaps | yes |
scale-down | like none or contain, whichever is smaller: never enlarged | yes |
The box itself doesn't change: object-fit only changes how the content is painted inside it. Borders, padding and the background belong to the element box, and the background shows in the gaps left by contain.
The most common pattern is uniform image tiles from photos of mixed shapes:
.gallery img {
width: 100%;
aspect-ratio: 1;
object-fit: cover;
}
object-position
object-position places the picture inside the box, using the same <position> syntax as background-position (article 29). The default is 50% 50%, centered. With cover, it chooses which part survives the crop:
.portrait { object-fit: cover; object-position: 50% 20%; } /* keep faces near the top */
.logo { object-fit: contain; object-position: left center; }
It's animatable, which allows simple panning effects.
object-view-box
object-view-box crops or zooms into a region of an image, like an SVG viewBox: object-view-box: inset(10% 20% 10% 0). It's in Chrome and in the Safari 27.3 beta, not in Firefox, so treat it as an enhancement.
What's current
aspect-ratiohas been in all browsers since 2021.object-fitandobject-positionwork everywhere.- Browsers map
widthandheightattributes toaspect-ratio: auto w / hfor images and videos. object-view-boxis in Chrome and the Safari beta.
Common questions
Why is my aspect-ratio ignored?
Both width and height are set, or the box is stretched in both axes by its layout, such as a grid item with both align-self: stretch and justify-self: stretch. By default, grid items with a ratio aren't stretched. Leave one dimension auto.
Why is my square box taller than it is wide?
Its content is taller than the ratio allows, so the box grows. Add min-height: 0 with an overflow value, or reduce the content.
What's the difference between object-fit: cover and background-size: cover?
They crop the same way. object-fit works on an <img> or <video> in the HTML, which can have alternative text and responsive sources. background-size (article 48) works on CSS backgrounds, which are decorative.
Do I still need width and height attributes if I size images with CSS?
Yes. They give the browser the ratio before the image loads, which reserves space and prevents layout shift. CSS can still change the actual size.
Does object-fit work on a <div> with a background?
No. It applies only to replaced elements. For backgrounds, use background-size and background-position.
Summary
aspect-ratio: w / hsizes an automatic dimension from the other one. If both are fixed, it's ignored.- The ratio applies to the
box-sizingbox. Content that doesn't fit makes the box taller unlessmin-height: 0oroverflowis set. widthandheightattributes on images becomeaspect-ratio: auto w / h, which prevents layout shift.object-fitdraws replaced content withfill,contain,cover,noneorscale-down.covercrops,containleaves gaps.object-positionaligns the content and chooses which partcoverkeeps.
Practice questions
Write CSS for a responsive video embed (an <iframe>) that's always as wide as its container and 16.
Answer
.embed { width: 100%; aspect-ratio: 16 / 9; height: auto; border: 0; }
A product grid has photos of mixed shapes. Make every photo a 4 tile that's fully filled, keeping the top of each photo visible.
Answer
.product img { width: 100%; aspect-ratio: 4 / 5; object-fit: cover; object-position: top; }
A <div> has the rule below, and no box-sizing rule. How big is its border box?
div {
width: 300px;
aspect-ratio: 3 / 1;
padding: 20px;
}
Answer
With content-box, the ratio applies to the content box: 300px by 100px. Adding padding, the border box is 340px by 140px. With border-box, it would be exactly 300px by 100px.
Why does the page jump when images load, and what HTML change fixes it?
Answer
Without known dimensions, images have no height until they load, so content below them moves down when they arrive. Add width and height attributes with the image's real dimensions: the browser uses them as an auto aspect ratio and reserves the space.
Previous: Sizing Boxes: width, height, Logical Sizes, min/max and Intrinsic Sizes
Next: Padding and Margins: Percentages, auto Centering and Negative Margins

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.