Explore Bato Web Agency Webflow Certified Partner Profile Explore Bato Web Agency Webflow Certified Partner Profile

What is the difference between object-fit: cover and contain?

Illustration without a description Illustration without a description

object-fit: cover fills the image box while preserving the source aspect ratio, so content that extends beyond the box is cropped. object-fit: contain also preserves the aspect ratio, but scales the complete image inside the box. Any unused area shows the element’s background. Choose between cropping and empty space.

 

Cover works well for photographic cards, avatars and hero media where every box must have the same shape. Cropping can remove content at the top, bottom or sides, depending on the source and container ratios. Contain is a better default for logos, product packshots, diagrams and screenshots when losing an edge would hide information.

 

  • Use cover when filling the frame matters and the edges are expendable.
  • Use contain when the whole image must remain visible.
  • Use a normal responsive image with height: auto when the box does not need a fixed shape.

 

Both values act on the content inside a replaced element such as img or video. The element must have a constrained width and height, or an aspect ratio that produces both dimensions. If the image box simply follows the file’s natural ratio, switching between cover and contain may show no visible change.

 

.thumbnail {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
object-position: 50% 35%;
}

.product-logo {
width: 100%;
height: 12rem;
object-fit: contain;
background: #f5f5f5;
}

 

The default position is centered. Pair cover with object-position when the subject sits away from the center, but test each image because one shared coordinate rarely fits a varied media library. Contain may produce letterboxing or pillarboxing; set a suitable background and padding instead of stretching the source to remove those areas.

 

Inspect the component at its smallest and largest supported sizes. A crop that looks harmless on a desktop card may cut off a face or text on a narrow phone. If no object position protects the subject across the range, provide an art-directed crop through picture. Keep informative text out of raster images whenever possible so cropping and scaling cannot make the text unreadable.