Before these two properties existed, a placeholder box with a fixed aspect ratio required a padding-top percentage hack, and cropping images to fill a box could not be controlled with pure CSS at all. These two modern properties turn both high-frequency needs into a single declaration.
aspect-ratio: Fixing the Aspect Ratio
aspect-ratio lets an element compute its height (or width) automatically from a specified ratio, with no need for the percentage-padding hack.
Old approach, for comparisonIn the past the same effect required position: relative on the outer element, ::before { padding-top: 56.25%; } on an inner element to prop up the height, and then absolutely positioning the real content with position: absolute to fill it — three lines reduced to one, with far clearer semantics.
Using It with Responsive Widths
A common image/video placeholder scenario: the width follows the container responsively, while the height is computed from the ratio, avoiding the page "jump" (cumulative layout shift, CLS) before and after the image finishes loading.
img {
width: 100%;
aspect-ratio: 4 / 3; /* reserves the correct height even before the image loads */object-fit: cover;
}
object-fit: How Media Fills Its Box
object-fit decides how replaced elements such as <img>/<video> fill their box when the element's own size does not match the media's native size; its semantics are very similar to background-size.
Value
Effect
fill (default)
Stretched to fill the box; may distort the image
cover
Scaled proportionally to cover the box completely, cropping the overflow; most commonly used
contain
Scaled proportionally to fit entirely; may leave blank space in the box
none
Keeps the native size, no scaling
object-fit comparison (color blocks simulate media with a wider native ratio)
cover (scaled and cropped)
contain (scaled with letterboxing)
object-position
Used together with object-fit: cover, it controls which part of the media stays visible when cropped — for example, cropping a portrait while always keeping the head instead of a centered crop.
.avatar {
object-fit: cover;
object-position: top center; /* keeps the top region of the image */
}
This is the most common pattern for card lists and article cover images: whatever the native ratio of the source image, the card cover always keeps a uniform 16:9 appearance.
Common Pitfalls
aspect-ratio has no effect when both an explicit width and height are set — it only applies when at least one dimension is auto-computed.
object-fit only affects replaced elements such as images and video, not background images on ordinary divs; use background-size for background images.
Cropping a portrait with object-fit: cover without setting object-position defaults to a centered crop that may cut off the head.