Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Filters, Masks and Shapes

The object-fit property

Reference for the CSS object-fit property: how images, videos and canvases fit their box with fill, contain, cover, none and scale-down, with live examples.

The object-fit property tells a replaced element (such as <img>, <video>, <canvas> or <iframe>) how its content should be resized to fit the element's box. Without it, an image forced into a box with a different shape is stretched. With object-fit, you choose between stretching, shrinking to fit, covering the whole box, or leaving the content at its natural size.

It is the <img> counterpart of background-size, and cover is the usual answer to "make every thumbnail the same size without distortion".

CSS
img.thumb {
  width: 200px;
  height: 120px;
  object-fit: cover;
}

Try it

Five ways to fit one image

The dashed outline is the element's box. fill stretches the image, contain shows all of it with empty bands, cover fills the box and crops, none keeps the natural size (cropped here), and scale-down picks the smaller of none and contain.

Syntax

CSS
object-fit: fill | contain | cover | none | scale-down;
Initial value fill
Applies to Replaced elements
Inherited No
Animatable No (discrete)
Computed value As specified

Values

Value Description
fill Stretches the content to exactly fill the box, ignoring its aspect ratio. This is the default.
contain Scales the content, keeping its aspect ratio, until it fits entirely inside the box. Leaves empty space on one axis if the shapes differ.
cover Scales the content, keeping its aspect ratio, until it covers the whole box. The overflow is clipped.
none Does not resize the content. It is shown at its natural size and clipped to the box.
scale-down Behaves like none or contain, whichever gives the smaller result. Large images shrink, small ones stay small.

Usage notes

  • Pair it with a fixed box. object-fit only matters when the width and height produce a shape different from the content. If you set just one dimension, the aspect ratio is preserved anyway.
  • Choose the focal point. Use object-position to decide which part of a covered or noned image stays visible; the default is the center.
  • Empty bands show the background. With contain, the unused area shows the element's background, so a background color makes it look intentional.
  • Cropping hides content. cover can cut off faces or text in the image. Pick the focus point carefully, and keep the alt text accurate, because it describes the whole image, not the visible part.
  • Layout shift. Set width, height or aspect-ratio so the browser reserves space before the image loads.
  • Background images are controlled with background-size, not object-fit.

Browser support

Supported in all current browsers. Very old browsers such as Internet Explorer lack it.