Definition and Usage
The object-fit
property specifies how content inside replaced elements, such as images or videos, is adjusted to fit the dimensions of their parent container.
Basic Example

Formal syntax
selector {
object-fit: fill | contain | cover | none | scale-down;
}
Formal definition
The object-fit
property specifies how the content of a replaced element—such as an image or video—should be resized to fit its container, based on the container’s width and height.
It is commonly applied to elements like <img>
and <video>
.
Using object-fit
, you can control how images or videos are fitted within their parent element.
Initial value | fill |
---|---|
Applies to | replaced elements |
Inherited | no |
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 |