On this page
A gallery can contain landscape photographs, portrait illustrations, and square product shots. Giving every image the same height distorts some assets; letting every image choose its own height creates uneven frames. CSS can separate the shape of the frame from the way media fits inside it.
Calculate the frame rather than stretch the asset
Use the Aspect Ratio Calculator to translate a desired ratio into concrete dimensions. At a width of 320 pixels, a four-to-three frame is 240 pixels tall, while a sixteen-to-nine frame is 180 pixels tall. These numbers help you compare crops before writing layout rules.
The key distinction is frame geometry versus source geometry. A portrait image placed inside a wide frame must either crop, leave empty space, or distort. CSS cannot preserve all source pixels, fill every frame, and maintain the original proportions at the same time when the ratios differ.
Decide which compromise is appropriate for the content. Product documentation often needs the complete object visible. Editorial photography can sometimes tolerate a crop. Screenshots containing text should rarely lose edges merely to produce identical thumbnails.
Let aspect-ratio supply an automatic dimension
The aspect-ratio property participates in sizing when at least one dimension is automatic. If you explicitly fix both width and height, the preferred ratio generally does not determine those dimensions. Start with a known inline size and leave the block size automatic.
.media-frame {
aspect-ratio: 16 / 9;
background: #eef0f3;
overflow: hidden;
}
.media-frame > img,
.media-frame > video {
display: block;
inline-size: 100%;
block-size: 100%;
object-fit: cover;
}
.media-frame--complete > img {
object-fit: contain;
}
.article-image {
display: block;
max-inline-size: 100%;
block-size: auto;
}The framed version deliberately creates a fixed proportion. The article image follows its own intrinsic proportions instead. Both are responsive, but they solve different presentation requirements. Do not apply the same global image rule to every media use case.
Read MDN’s aspect-ratio reference for how preferred ratios interact with automatic sizing. Minimum and maximum size constraints can override the size you expected from the ratio, particularly inside flexible layouts.
Choose cover or contain deliberately
object-fit: cover preserves the media’s proportions while filling its content box. Some pixels fall outside that box when source and frame ratios differ. contain preserves the entire source but may leave empty space along one axis.
Neither setting changes the shape of the element’s box. Object fitting controls the replaced content inside that box. This is why setting only object-fit: cover on an image with no constrained frame may appear to do nothing useful: there is no mismatched box for the image to fill.
Use a visible background with contain so the empty region looks deliberate. For transparent product images, choose that background carefully because the source may already include transparent margins. An image editor’s canvas and the visible object are not necessarily the same dimensions.
The MDN object-fit documentation also describes fill, none, and scale-down. The default fill can stretch content when both dimensions are constrained. Avoid relying on it for photographs unless distortion is explicitly acceptable.
Control the crop with object-position
A centered crop works poorly when the subject sits near an edge. Use object-position to move the source within the frame. A portrait might use 50% 25% to favor its upper area, but that number is a content decision rather than a universal rule for every face.
Store a focal point with the asset when editors need predictable control. A component can read custom properties such as --focus-x and --focus-y, then apply object-position: var(--focus-x, 50%) var(--focus-y, 50%). Keep defaults so missing metadata still produces valid CSS.
Inspect both the wide and narrow frames. A position that protects the subject in a square crop may cut it off in a panoramic crop. If composition changes radically, a separate source image may be more appropriate than trying to rescue every layout with positioning.
For videos, consider subtitles and text embedded in the picture. Cropping can remove information, not just decoration. Give essential media a complete-view variant or an uncropped presentation rather than treating every clip like a decorative background.
Check intrinsic sizes and layout constraints
HTML image width and height attributes can communicate the source’s intrinsic proportions before the image is decoded. CSS can still make the image responsive. Keep those attributes aligned with the actual source geometry rather than writing the thumbnail frame dimensions into every unrelated asset.
An image styled with aspect-ratio: auto 4 / 3 can use the supplied ratio before its intrinsic ratio is available, then use the natural ratio afterward. That differs from a deliberate fixed-ratio wrapper. Understand which behavior you want before using the auto fallback ratio pattern.
See MDN’s object-position reference when adjusting alignment. Also test how grid and flex constraints interact with media. A large intrinsic minimum size can force a track wider than expected unless the relevant item can shrink.
Use min-inline-size: 0 on the appropriate flex or grid item when necessary. Do not scatter it everywhere without understanding the sizing path. The existing Grid and Flexbox guide provides context for the parent layout decision, while this article focuses on the media box itself.
Build a small fixture containing a wide photograph, tall photograph, transparent PNG, SVG, failed request, and video poster. These expose source variation that a single attractive demo image conceals. Check the result with long captions too; the frame should not accidentally constrain the caption’s height.
Finally, document the allowed ratios and fit modes with your other HTML and CSS references. A clear API such as wide-cover, square-cover, and complete-content is easier to maintain than arbitrary widths and heights passed independently at every call site.
Key takeaways
- Separate the frame’s ratio from the source image’s intrinsic ratio.
- Keep a dimension automatic when aspect-ratio should determine sizing.
- Use cover for intentional crops and contain for complete-source visibility.
- Store focal points when centered crops are not reliable.
- Test varied source shapes and inspect parent layout constraints.
Reach designers & developers at work
Put your product next to the tools people use every day. One sponsor per placement, no tracking pixels.
Become a sponsor