Aspect Ratio and Object Fit in Layout
Images and video have a fundamental tension with grid and flex layouts. A track can be any size the browser computes, but a photograph has a fixed ratio of width to height. If the image stretches to fill its track, it distorts. If it shrinks to preserve its ratio, it leaves gaps. CSS provides two complementary tools to resolve this: aspect-ratio enforces a ratio on any element, and object-fit controls how replaced content fills its container without distortion. Together they make media behave predictably inside any layout context.
The Problem Without These Properties
When you place an <img> inside a grid cell, the browser stretches it to fill the cell by default because grid items stretch along the block axis. The image maintains its intrinsic width but its height gets forced to match the cell, distorting the aspect ratio. Setting width: 100% fixes the width but then the height is determined by the image's intrinsic ratio, which means cells in the same row will have different heights if their images have different proportions.
Before aspect-ratio, developers used the padding-top hack to force a ratio on a container. You set padding-top: 56.25% to get 16:9 because padding percentages are calculated against the element's width. Then you absolutely positioned the image inside. It worked but was fragile, unintuitive, and failed when the container was unknown. The modern approach is a single property.
aspect-ratio
The aspect-ratio property takes a width-to-height ratio expressed as two numbers separated by a slash. The browser uses it to compute the missing dimension when only one dimension is set:
.video-container {
width: 100%;
aspect-ratio: 16 / 9;
}
.square-thumb {
width: 80px;
aspect-ratio: 1; /* shorthand for 1 / 1 */
}
The key insight is that aspect-ratio works on any element, not just images. A <div>, a grid cell, a flex item, a replaced element like <video> or <iframe> all respond to it. This makes it the right tool for constraining the shape of a container before placing content inside it, as well as for ensuring images and video maintain their ratio as their container resizes.
When an image has both width: 100% and no explicit height, it already respects its intrinsic ratio. aspect-ratio is most useful when you need to enforce a specific ratio that may differ from the image's natural proportions, or when you need to size a non-replaced element like a <div> proportionally.
If a replaced element like <img> has an intrinsic ratio and you also set aspect-ratio, the intrinsic ratio wins unless the image is sized explicitly in both dimensions. aspect-ratio is most reliable on non-replaced elements or when you set width: 100% and let the height be computed.
object-fit and object-position
When a replaced element like <img> or <video> sits inside a container with a fixed size or aspect ratio, object-fit controls how its content fills that space. It works like background-size but for replaced elements.
The five values cover the common scenarios:
-
fill(default): stretches the content to fill the container exactly, distorting the ratio if the container is a different proportion. -
cover: scales the content up until it covers the entire container, cropping the overflow. The ratio is preserved. This is the most common value for card thumbnails and hero images. -
contain: scales the content down until it fits entirely inside the container without cropping. The ratio is preserved but there may be empty space (letterboxing). -
none: renders the content at its intrinsic size with no scaling. Content may overflow or underlap the container. -
scale-down: behaves likenoneorcontain, whichever is smaller.
object-position controls where the content is anchored within its container when it does not fill it exactly, or when cover crops it. It accepts the same values as background-position: keywords like center, top left, percentages, and lengths:
.card-thumb {
width: 100%;
aspect-ratio: 3 / 2;
object-fit: cover;
object-position: center top; /* preserve top of image when cropped */
}
Combining Both Properties
The pattern that handles nearly every card thumbnail situation is:
.card img {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: center;
display: block;
}
The image fills its container's full width, is always 16:9, crops rather than distorts, and centers the crop. Changing aspect-ratio to 4 / 3 or 1 changes the card thumbnail shape without touching any other rules.
Card Thumbnails That Refuse to Stretch
The demo below shows a card grid where each card contains a photograph. Use the controls to switch between object-fit values and adjust the aspect ratio. Notice how cover crops cleanly while fill distorts the proportions, contain letterboxes, and none lets the image overflow its box.
Named Grid Areas
Layout with readable area names
Responsive Tracks
auto-fit with minmax patterns
Intrinsic Sizing
Content-driven track widths
Logical Properties
Direction-aware spacing
Hero Video Boxes That Keep 16:9
A full-width video or iframe embed must stay at 16:9 as the page scales. Before aspect-ratio, this required the padding-top hack. Now it takes two rules:
.video-embed {
width: 100%;
aspect-ratio: 16 / 9;
}
.video-embed iframe,
.video-embed video {
width: 100%;
height: 100%;
display: block;
}
The container takes full width and computes its own height from the ratio. The iframe or video inside fills the container completely. No JavaScript, no padding tricks, no fixed pixel heights. Resizing the viewport keeps the ratio exact.
The same pattern works for any embed, including maps, code sandboxes, or third-party widgets that use iframes. It also works for <video> elements directly: you can set aspect-ratio: 16 / 9 and object-fit: cover on the video element itself and it will fill its container and crop rather than letterbox.
Live Hero Container Demo
The container below holds a wide photograph and keeps its shape with aspect-ratio. Resize your browser window or change the ratio selector to see the container reshape while the image fills it with object-fit: cover.
object-position in Practice
When object-fit: cover crops an image, the default anchor is the center. That is correct for most subjects but wrong for portrait photography where the subject's face is near the top, or for product photography where the key detail is in a corner. object-position lets you shift the anchor:
/* Portrait: preserve the top of the image */
.portrait-thumb {
aspect-ratio: 1;
object-fit: cover;
object-position: center top;
}
/* Product with logo in bottom-right corner */
.product-thumb {
aspect-ratio: 4 / 3;
object-fit: cover;
object-position: right bottom;
}
/* Precise percentage offset */
.hero-image {
aspect-ratio: 21 / 9;
object-fit: cover;
object-position: 50% 30%; /* slightly above center */
}
Content-managed sites where editors upload arbitrary images benefit most from explicit object-position values. A card grid that always crops to 16:9 will center-crop a landscape photo correctly but may cut off a person's head in a portrait photo. Setting object-position: center top as a default for people-focused content avoids the most common failure case without needing per-image overrides.
Anchor Point Demo
Pick an anchor to zoom into that region of the photograph. The demo uses a large image and a simple scale transform to illustrate what object-position does when a frame crops the overflow.
Object-Position Anchor
object-position: center
Teaching Video Checklist
When you record your teaching video, make sure your teammates come away understanding these concepts:
-
Why images distort inside grid or flex tracks by default and how
aspect-ratioreplaces the padding-top hack -
The difference between
object-fit: cover,contain, andfill, demonstrated on the same image in a fixed-ratio container -
How
object-positioncontrols which part of the image stays visible when cropped -
The card thumbnail recipe:
width: 100%,aspect-ratio, andobject-fit: coverworking together - At least one image or video embed on your portfolio updated to use these properties inside a grid or flex layout
Start with a distorted image in a grid cell, then apply aspect-ratio and object-fit: cover step by step so teammates can see which property fixes which problem.
Resources
- MDN: aspect-ratio — Reference for ratio-driven sizing on any element
- MDN: object-fit — How replaced content fills its container
- MDN: object-position — Anchoring cropped or letterboxed media
- CSS-Tricks: object-fit — Visual comparison of fit modes
- Can I Use: aspect-ratio — Current browser support data