Definition and Usage
The object-fit
property specifies how the content of a replaced element, such as an image or video, is fitted within the element's content box based on its specified width and height.
This property lets you control how images and videos are resized to fit an element’s dimensions, preserving their natural appearance without distortion.
Basic Example

Formal syntax
selector {
object-fit: fill | contain | cover | none | scale-down;
}
Formal definition
The object-fit
property specifies how the content (object) of a replaced element, such as an image or video, is fitted within the specified content-box
, based on its height and width. It is primarily used with <img>
and <video>
elements.
Initial value | fill |
---|---|
Applies to | replaced elements |
Inherited | no |
Percentages | n/a |
Computed value | specified keyword |
Animatable | no |
Values
fill |
Initial value.
The image or video is stretched to completely fill the container. The aspect ratio is not preserved, so distortion may occur. |
---|---|
contain |
The content is resized to fit inside the container while maintaining its aspect ratio. It will be fully visible, but may leave empty space on one axis. |
cover |
The content is scaled to fill the entire container while maintaining its aspect ratio. Some parts may be clipped if the aspect ratio does not match the container. |
none |
The content keeps its original size. It is not resized to fit the container. |
scale-down |
The content is scaled down to the smaller result of either none or contain . If the intrinsic size is smaller than the container, it behaves like none . Otherwise, it behaves like contain . |
Practical Examples
The following examples demonstrate how different object-fit
values affect image and video elements.
object-fit: fill
Initial value.
The image or video is stretched to completely fill the container. The aspect ratio is not preserved, so distortion may occur.
Image Example
<div class="container">
<img src="flower.jpg" alt="flower">
</div>
.container {
width: 300px;
height: 100px;
border: 5px dashed red;
}
img {
width: 100%;
height: 100%;
object-fit: fill;
}

Video Example
<div class="container">
<video src="flower.webm" autoplay muted playsinline loop></video>
</div>
.container {
width: 300px;
height: 100px;
border: 5px dashed red;
}
video {
width: 100%;
height: 100%;
object-fit: fill;
}
object-fit: contain
The content is resized to fit inside the container while maintaining its aspect ratio. It will be fully visible, but may leave empty space on one axis.
Image Example
<div class="container">
<img src="flower.jpg" alt="flower">
</div>
.container {
width: 300px;
height: 100px;
border: 5px dashed red;
}
img {
width: 100%;
height: 100%;
object-fit: contain;
}

Video Example
<div class="container">
<video src="flower.webm" autoplay muted playsinline loop></video>
</div>
.container {
width: 300px;
height: 100px;
border: 5px dashed red;
}
video {
width: 100%;
height: 100%;
object-fit: contain;
}
object-fit: cover
The content is scaled to fill the entire container while maintaining its aspect ratio. Some parts may be clipped if the aspect ratio does not match the container.
Image Example
<div class="container">
<img src="flower.jpg" alt="flower">
</div>
.container {
width: 300px;
height: 100px;
border: 5px dashed red;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}

Video Example
<div class="container">
<video src="flower.webm" autoplay muted playsinline loop></video>
</div>
.container {
width: 300px;
height: 100px;
border: 5px dashed red;
}
video {
width: 100%;
height: 100%;
object-fit: cover;
}
object-fit: none
The content keeps its original size. It is not resized to fit the container.
Image Example
<div class="container">
<img src="flower.jpg" alt="flower">
</div>
.container {
width: 300px;
height: 100px;
border: 5px dashed red;
}
img {
width: 100%;
height: 100%;
object-fit: none;
}

Video Example
<div class="container">
<video src="flower.webm" autoplay muted playsinline loop></video>
</div>
.container {
width: 300px;
height: 100px;
border: 5px dashed red;
}
video {
width: 100%;
height: 100%;
object-fit: none;
}
object-fit: scale-down
The content is scaled down to the smaller result of either none
or contain
. If the intrinsic size is smaller than the container, it behaves like none
. Otherwise, it behaves like contain
.
Image Example
<div class="container">
<img src="flower.jpg" alt="flower">
</div>
.container {
width: 300px;
height: 100px;
border: 5px dashed red;
}
img {
width: 100%;
height: 100%;
object-fit: scale-down;
}

Video Example
<div class="container">
<video src="flower.webm" autoplay muted playsinline loop></video>
</div>
.container {
width: 300px;
height: 100px;
border: 5px dashed red;
}
video {
width: 100%;
height: 100%;
object-fit: scale-down;
}
Browser compatibility
Property |
Desktop Chrome
|
DesktopDesktop Edge
|
Desktop Firefox
|
Safari
|
---|---|---|---|---|
object-fit
|
32 | 79 | 36 | 10 |
Specifications
Specification | |
---|---|
object-fit
|
CSS Images Module Level 3 #the-object-fit |