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".
img.thumb { width: 200px; height: 120px; object-fit: cover; }
Try it
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
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-fitonly 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-positionto decide which part of acovered ornoned image stays visible; the default is the center. - Empty bands show the background. With
contain, the unused area shows the element'sbackground, so a background color makes it look intentional. - Cropping hides content.
covercan cut off faces or text in the image. Pick the focus point carefully, and keep thealttext accurate, because it describes the whole image, not the visible part. - Layout shift. Set
width,heightoraspect-ratioso the browser reserves space before the image loads. - Background images are controlled with
background-size, notobject-fit.
Browser support
Supported in all current browsers. Very old browsers such as Internet Explorer lack it.
Related
object-positionโ positions the content inside the boxaspect-ratioโ reserves a shape for the boxbackground-sizeโ the equivalent for background images- Lessons: Object fit, Styling images