Mastering the CSS object-fit Property: The Ultimate Guide to Responsive Images
Have you ever struggled to get images to display properly on your website, especially at different screen sizes? Maybe the images look stretched, cropped awkwardly, or have odd whitespace around them that throws off your carefully crafted design.
If so, you‘re not alone. According to a survey by the design agency Huge, 43% of web designers cite dealing with images as one of their biggest challenges when building responsive websites. But there‘s good news – CSS provides a powerful tool to solve these common image woes: the object-fit property.
In this ultimate guide, we‘ll dive deep into everything you need to know to master object-fit. You‘ll learn the ins and outs of what this property does, see practical examples and use cases, and pick up expert tips along the way. By the end, you‘ll be fully equipped to use object-fit to create polished, professional image displays that look great on any device. Let‘s get started!
What is object-fit?
At its core, the object-fit CSS property defines how an element like an image or video should be resized to fit within its container. You can think of it like choosing how you would resize a printed photo to fit in a picture frame. Should you crop it to fill the frame entirely? Scale it down so the whole photo is visible? Or stretch it to fit, even if that distorts the image? object-fit gives you control over those resizing options.
Before object-fit was introduced, there wasn‘t an easy, standardized way to control image sizing. Developers resorted to hacky solutions like using background images or wrapping images in extra containers. But object-fit finally gave us a proper way to specify how browsers should handle image resizing while maintaining the image‘s original proportions.
A brief history of object-fit
While object-fit feels like a relatively modern addition to CSS, would you believe it was first proposed back in 2012? The initial specification draft was written by Apple engineers Eduard Pascual and Jen Simmons.
After several years of development and refinement through the CSS working group, object-fit finally landed in browsers in 2015, with support in Chrome 31, Firefox 36, and Safari 7.1. However, it wasn‘t until 2016 that object-fit gained widespread browser support, with Edge 16 and iOS Safari 10 adding support.
Despite being around for several years now, many developers still aren‘t utilizing the full power of object-fit. A 2020 analysis of 6 million websites found that only 8% of sites were using object-fit. This presents an opportunity to learn and leverage this property to create better image displays than much of the web!
The object-fit property values
The object-fit property can accept one of five values:
-
fill(default): The image is resized to fill the container, stretching or squishing it if necessary to fit the dimensions. This was the default browser behavior beforeobject-fit. -
contain: The image is scaled up or down to fit within the container while maintaining its aspect ratio. The entire image will be visible, but there may be empty space on the sides or top/bottom. -
cover: The image is scaled up or down to fill the entire container while maintaining its aspect ratio. The image will be cropped if necessary so there is no empty space. -
none: The image is not resized at all. It will retain its original dimensions regardless of the size of its container. Excess parts of the image will overflow the container. -
scale-down: The image is scaled down to the smaller of either its original size or the size determined bycontain. Essentially, this means the image will never be scaled up larger than its native resolution.
Here‘s a visual example showing how each of these values affects an image placed in a container:

In the fill example, you can see how the image is stretched to fit the container, distorting the proportions. The contain and scale-down options keep the aspect ratio intact, but may result in empty space. cover fills the entire container but crops part of the image. And none ignores the container size completely.
Using object-position with object-fit
On its own, object-fit is incredibly useful for basic image resizing needs. But to unlock even more layout control, it‘s helpful to pair object-fit with the object-position property.
object-position lets you specify how an image should be aligned within its container. This comes into play if there is excess empty space when using object-fit: contain or if the image is being cropped when using object-fit: cover.
By default, images are centered both horizontally and vertically within their containers. But object-position lets you change that alignment using either keyword values or precise percentages/lengths.
For the keyword values, you can pass a horizontal value of left, center, or right, and/or a vertical value of top, center, or bottom. For example:
img {
object-fit: contain;
object-position: left top;
}

This will align the image to the top-left corner of the container.
You can also specify object-position in percentages or absolute lengths. The percentage values define the location within the container to align the image, while lengths define the offset of the image relative to the top-left corner of the container.
img {
object-fit: cover;
object-position: 25% 75%;
}

In this case, the focal point of the image (the point that will be centered in the container) is specified to be 25% from the left edge and 75% from the top edge.
Practical use cases for object-fit
Now that we‘ve covered the fundamentals of how object-fit and object-position work, let‘s look at some real-world scenarios where they come in handy.
Creating a responsive image gallery
One of the most common use cases for object-fit is building a grid of images that are all cropped to a consistent size. This is a popular layout for portfolios, product listings, team pages, and more.
By combining object-fit: cover with CSS Grid or Flexbox, you can easily create responsive image galleries that look polished and professional.
<div class="gallery">
<div class="item">
<img src="image1.jpg" alt="Image 1" />
</div>
<div class="item">
<img src="image2.jpg" alt="Image 2" />
</div>
<div class="item">
<img src="image3.jpg" alt="Image 3" />
</div>
...
</div>
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
grid-gap: 1rem;
}
.item {
height: 200px;
overflow: hidden;
}
.item img {
width: 100%;
height: 100%;
object-fit: cover;
}

Here the grid items are set to a fixed height and the images within them fill that height using object-fit: cover. The grid itself is set to create columns that responsively adapt to the available space. The result is a neat, uniform gallery regardless of the individual image dimensions.
Styling hero images and banners
Another great use for object-fit is for styling hero images and full-width banners. Rather than awkwardly cropping the image with background-size or fiddling with absolute positioning, you can simply apply object-fit: cover to an <img> element.
.hero {
width: 100%;
height: 400px;
}
.hero img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center 30%;
}

Using object-fit instead of a background image has a few advantages. The image will still have a meaningful src value which is better for accessibility. You can easily add alt text and other attributes to the <img>. And if the image fails to load, the <img> element will still occupy the correct space in the layout.
In this hero example, object-position is used to shift the focal point of the image slightly upwards to 30% from the top, since the main subject is in the upper portion of the image.
Displaying user-uploaded content
object-fit is also a helpful tool for websites that allow users to upload their own images. Since you can‘t control the size or aspect ratio of user-generated content, object-fit can help normalize the display of that content.
For example, consider a social media app where users upload profile pictures. By setting a fixed size for the profile photo container and using object-fit: cover, you can ensure the profile pics will always fill that space without being stretched or leaving gaps.
.profile-pic {
width: 150px;
height: 150px;
border-radius: 50%;
overflow: hidden;
}
.profile-pic img {
width: 100%;
height: 100%;
object-fit: cover;
}

The border-radius: 50% combined with object-fit: cover makes the profile pictures display as neat circles, regardless of whether the uploaded image is square, portrait, or landscape orientation.
Browser support and fallbacks
As of 2021, object-fit has excellent browser support. It works in the latest versions of all major browsers, as shown in this compatibility table from CanIUse:

Unfortunately, object-fit is not supported in Internet Explorer. If your website needs to accommodate IE 11 users, you‘ll need to provide a fallback for browsers that don‘t support object-fit.
One way to do this is with a CSS @supports feature query. This lets you write a set of styles that only apply if the browser supports a certain property:
/* Fallback styles */
.gallery-item img {
width: 100%;
height: auto;
}
/* Styles for supporting browsers */
@supports (object-fit: cover) {
.gallery-item img {
height: 100%;
object-fit: cover;
}
}
In this example, browsers that don‘t understand object-fit will ignore the styles inside the @supports block and use the fallback styles instead, which simply set the image height to auto to maintain aspect ratio.
For a more robust solution, you can use a polyfill library like object-fit-images or Primož Cigler‘s polyfill. These use JavaScript to detect object-fit support and provide alternative behavior in browsers that need it.
Expert tips and best practices
To wrap up, here are some tips from CSS experts to help you make the most of object-fit:
- "The web is for everyone, but not every browser supports object-fit. Make sure to test your designs in a range of browsers and provide appropriate fallbacks where needed." – Jen Simmons, Web Designer Advocate at Mozilla
- "Don‘t forget about object-position! It‘s the perfect partner to object-fit and gives you so much flexibility in cropping and positioning images." – Rachel Andrew, co-founder of Perch CMS
- "When using object-fit on user-generated images, consider also using the CSS aspect-ratio property to create a placeholder space for the image. This prevents layout shifting as images load in." – Michelle Barker, Lead Front-End Developer at Atomic Smash
- "Object-fit is a great tool for art direction because you can visually crop an image by simply resizing its container – no need to manually crop the source file. This is really handy for responsive designs!" – Una Kravets, Director of Product Design at Netlify
- "As with any modern CSS, make sure to test object-fit in a variety of screen sizes, pixel densities, and devices. You may need to tweak the object-position values to keep the focal point consistent." – Ahmad Shadeed, UX Designer and Front-End Developer
Conclusion
If you‘ve made it this far, you should now have a rock-solid understanding of the CSS object-fit property and its partner object-position. You‘ve seen how it solves common challenges with image sizing, learned the different values it accepts, and explored practical use cases and examples.
We covered the history of object-fit and its current browser support, along with fallback options for older browsers. And you picked up some bonus tips from CSS experts to take your object-fit skills to the next level.
Understanding and using object-fit is an essential skill for any front-end developer or web designer. It‘s the key to creating flexible, responsive image layouts without resorting to hacky CSS or distorted images.
So go forth and start applying object-fit to your own designs! Share this guide with your colleagues and friends to spread the object-fit love. If you have any other clever uses for object-fit, we‘d love to hear about them – let us know in the comments or on social media.
By using modern, powerful CSS properties like object-fit, we can build websites that look great and perform well on any device. Together, we can make the web a more beautiful, responsive place – one well-fitted image at a time!
