The object-fit property controls how images, videos, and other replaced content fit inside a container. It is one of the most useful tools for responsive layouts because it prevents awkward stretching while keeping media looking polished.
01
Replaced Content
Works on images and video.
02
Syntax
One keyword controls fit.
03
cover
Fill and crop cleanly.
04
contain
Show the whole image.
05
fill Default
Stretch to the box.
06
Responsive UI
Cards, avatars, heroes.
Fundamentals
Introduction
The object-fit property in CSS is used to control how the content of a replaced element, such as an <img> or <video>, is resized to fit its container.
This property is particularly useful for maintaining aspect ratios and preventing distortion when images or videos are scaled to fit a specific size.
Definition and Usage
The object-fit CSS property tells the browser how replaced content should be sized relative to its box. You usually set explicit width and height on the image or its container, then choose a fit mode such as cover or contain.
This makes it easy to build card thumbnails, hero banners, profile photos, and video embeds that stay visually consistent across screen sizes.
💡
Beginner Tip
For most layouts, start with object-fit: cover when cropping is acceptable, and object-fit: contain when the entire image must remain visible.
Foundation
📝 Syntax
The syntax for the object-fit property is straightforward. It can be applied to any replaced element, such as an image, video, or iframe:
syntax.css
element{object-fit:value;}
Here, value determines how the content is resized to fit the container.
Set a defined width and height on the media element or its container.
Choose one keyword: fill, contain, cover, none, or scale-down.
Pair with object-position when you need to control cropping focus.
Use overflow: hidden on the container when cropped edges should not spill out.
Defaults
🎯 Default Value
The default value of the object-fit property is fill. This means the content will stretch to fill the entire container, which can sometimes result in distortion if the aspect ratio of the content does not match the container.
That is why many responsive layouts explicitly set object-fit: cover or object-fit: contain instead of relying on the default.
Cheat Sheet
⚡ Quick Reference
Question
Answer
Initial value
fill
Applies to
Replaced elements such as img, video, and iframe
Inherited
No
Animatable
No
Common use
Responsive thumbnails, hero images, avatars, and video frames
Reference
💎 Property Values
The object-fit property accepts five keyword values. Each one changes how content is scaled inside its box.
Value
Example
Description
fill
object-fit: fill;
Stretches content to fill the container. Aspect ratio may change.
contain
object-fit: contain;
Scales content to fit inside the box while keeping the full image visible.
cover
object-fit: cover;
Scales content to fill the box. Extra edges may be cropped.
none
object-fit: none;
Keeps the original size. Content is not resized.
scale-down
object-fit: scale-down;
Uses whichever is smaller: none or contain.
cover — crop to fillcontain — show allfill — stretchnone — original sizescale-down — shrink only
Scope
Supported Elements
object-fit is designed for replaced elements — content whose size comes from an external resource rather than CSS text flow:
<img> — the most common use case for cards, galleries, and avatars.
<video> — useful for background videos and fixed-ratio players.
<iframe> — can help embedded content fit responsive frames.
For best results, give the element a defined box using width, height, or a parent container with a fixed aspect ratio.
Preview
👀 Live Preview
The same landscape image is placed in identical frames. Compare how each object-fit value behaves:
fill
contain
cover
none
scale-down
Hands-On
Examples Gallery
Use object-fit to control how images and videos behave inside fixed-size containers.
🖼️ Responsive Images
These patterns appear constantly in card grids, profile photos, and hero sections.
Example 1 — Card Thumbnail with cover
In this example, an image fills a fixed-size container while keeping its aspect ratio. Extra edges are cropped instead of stretching.
The frame keeps a 16 / 9 ratio, and the video fills it without letterboxing when cover is used.
Companion
Pair with object-position
When you use object-fit: cover, part of the image may be cropped. The related object-position property lets you choose which area stays visible, such as object-position: top center for portrait photos or product shots.
Always provide alt text — Cropping with cover can hide important visual details, so describe the image meaningfully.
Do not hide critical content — If text or key subject matter gets cropped, adjust the frame ratio or use object-position.
Avoid distortion — Stretched images from fill can look unclear and reduce readability of embedded text or logos.
Test responsive layouts — A crop that looks fine on desktop may remove important content on mobile.
🧠 How object-fit Works
1
You define a media box
Set width and height on the image, video, or container so the browser knows the target area.
Layout
2
You choose a fit mode
Keywords like cover, contain, and fill tell the browser how to scale the media.
object-fit
3
The browser scales the content
The media is resized, cropped, or letterboxed inside the box while preserving aspect ratio when required.
Rendering
=
🖼️
Consistent responsive media
Images and videos look intentional across cards, grids, and hero sections.
Compatibility
Browser Compatibility
The object-fit property is supported in all modern browsers, including current versions of Chrome, Firefox, Safari, Edge, and Opera. Always test across the browsers your audience uses.
✓ Baseline · Modern browsers
Reliable support for responsive media
Chrome, Edge, Firefox, Safari, and Opera all support object-fit in current versions.
97%Modern browser support
Google Chrome31+ · Desktop & Mobile
Full support
Mozilla Firefox36+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Microsoft Edge16+ · Chromium & Legacy
Full support
Opera19+ · Modern versions
Full support
Fallback behavior
When unsupported, images fall back to default stretching behavior similar to fill.
💻
Internet ExplorerNo support · Use a sized container and background-image fallback if needed
None
object-fit property97% supported
Bottom line: Use object-fit confidently in modern responsive layouts. Pair with aspect-ratio for predictable media frames.
Wrap Up
Conclusion
The object-fit property provides a simple yet powerful way to control how content like images and videos are displayed within their containers.
By understanding and utilizing the various values of this property, you can ensure that your media elements look great across different screen sizes and layouts, enhancing the visual appeal and user experience of your website.