object-fit
Widely availableSince 202097% of users
Defines how an <img> or <video> should be resized to fit its container. cover makes images fill a space without being squashed.
What's modern
From web-features, MDN's browser data and our snippets, checked dailyModernIn every major browser since January 2020. Widely available since July 2022.
The old way and the modern waySee the snippet: Responsive images without the background-image hack →
Old way 8 lines
<!-- div instead of img: no alt, not semantic -->
<div class="card-image"></div>
.card-image {
background-image: url(photo.jpg);
background-size: cover;
background-position: center;
height: 200px;
}Modern 5 lines
<img src="photo.jpg" alt="..." loading="lazy">img {
object-fit: cover;
width: 100%;
height: 200px;
}See how modern your own CSS is.Check your CSS →
Quick facts
- Initial value
fill- Inherited
- No
- Applies to
- replaced elements
- Animation type
- discrete
Quick example
.profile-image {
width: 200px;
height: 200px;
object-fit: cover;
/* ensures the image fills the 200x200 square without distortion */
}Values
fillcontaincovernonescale-downRelated snippet
Responsive images without the background-image hackLayout →object-fit browser support
BaselineWidely availableThis feature is well established and works across many devices and browser versions. It has been available across browsers since 2020.Safe to use without fallbacks.
Usage97%of people can use this3% use a browser without it.
Browsers
32+
36+
10+
79+Mobile
32+Android
36+Android
10+iOS