CSS object-fitProperty
Example
Crop the image, preserving the original aspect ratio:
img.a {
width: 200px;
height: 400px;
object-fit: cover;
}
Try it »
Definition and usage instructions
The object-fit property specifies how the content of an element should fit the specified height and width of its container.
object-fit is generally used with img and video tags; these elements can be cropped or scaled while retaining the original aspect ratio, or directly stretched.
You can use theobject-positionproperty to switch the alignment of the content object of a replaced element within its element box.
| Default value: | fill |
|---|---|
| Inherited: | No. |
| Animatable: | No.AboutCSS Animation |
| Version: | CSS3 |
| JavaScript syntax: | object.style.objectFit="cover" Try it » |
Browser support
The numbers in the table indicate the first browser version number that supports the 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;
Property values
| Valor | Description | Try it |
|---|---|---|
| fill | Default. Does not guarantee preserving the original aspect ratio; content is stretched to fill the entire content container. | Try it » |
| contain | Maintains the original aspect ratio. Content is scaled. | Try it » |
| cover | Maintains the original aspect ratio. But some content may be cropped. | Try it » |
| none | Preserves the original element content's width and height, meaning the content will not be resized. | Try it » |
| scale-down | Maintains the original aspect ratio. The size of the content will be the same as either none or contain, whichever results in a smaller object size. | Try it » |
| initial | Set to the default value,Aboutinitial | |
| inherit | Inherits the property from the parent element.Aboutinherit |