CSS object-fit property
Example
Crop the image while preserving its original ratio:
img
.a
{
width:
200
px
;
height:
400
px
;
object-fit:
cover
;
}
Try it »
Tag Definition and Usage Notes
The object-fit property specifies how an element's content should be fitted to the given container's height and width.
object-fit is generally used with img and video tags, allowing these elements to be cropped or scaled while preserving their original ratio, or stretched directly, and so on.
You can use the object-position property to switch the alignment of a replaced element's content object within the element's box.
| Default value: | fill |
|---|---|
| Inherited: | none. |
| Animatable: | none. About CSS Animations |
| Version: | CSS3 |
| JavaScript syntax: | object.style.objectFit="cover" Try it » |
Browser support
The numbers in the table indicate the first browser version that supports this property.
| property | |||||
|---|---|---|---|---|---|
| object-fit | 31.0 | 16.0 | 36.0 | 7.1 | 19.0 |
Syntax
object-fit: fill|contain|cover|scale-down|none|initial|inherit;
Attribute values
| Value | Description | Try it |
|---|---|---|
| fill | Default. The original ratio is not preserved; the content is stretched to fill the entire content container. | Try it » |
| contain | Preserves the original size ratio. The content is scaled. | Try it » |
| cover | Preserves the original size ratio. However, part of the content may be cropped. | Try it » |
| none | Keeps the original length and width of the element's content, meaning the content is not resized. | Try it » |
| scale-down | Preserves the original size ratio. The content size is the same as either none or contain, whichever results in the smaller object size. | Try it » |
| initial | Sets it to the default value,About initial | |
| inherit | Inherits the property from the element's parent. About inherit |