Object fit in CSS controls how replaced content, such as an image or video, fits inside its own box. It is most commonly used with images that need to stay inside a fixed card, banner, avatar, gallery tile, or video frame without looking stretched or broken.
The important idea is that object-fit does not create the box by itself. You usually set a width and height first, then use object-fit to decide how the media should fill that space. This makes image layouts much easier to control in responsive designs.
What Is a Replaced Element?
The object-fit property works on replaced elements. A replaced element is an element whose content is replaced by an external object, such as an image, video, iframe, or embedded media. The browser knows the natural size of that external object, but CSS can force it into a different box.
img {
width: 320px;
height: 200px;
object-fit: cover;
}
Here the image element has a fixed visual box of 320px by 200px. The actual photo may have a completely different natural size, but object-fit decides how it appears inside that box.
object-fit: fill
The default value is fill. It stretches the content to fill the entire box, even if that changes the original aspect ratio. This can distort images.
.thumbnail {
width: 300px;
height: 180px;
object-fit: fill;
}
Use fill only when distortion is acceptable. For real photos, logos, and product images, distortion usually looks unprofessional.
object-fit: contain
The value contain scales the content so the whole object is visible inside the box. The original aspect ratio is preserved. If the box and media ratio do not match, empty space appears.
.product-image {
width: 100%;
height: 280px;
object-fit: contain;
}
This is useful for product images, diagrams, logos, certificates, and any media where cropping would remove important details. The tradeoff is that the box may show blank space around the object.
object-fit: cover
The value cover scales the content so the box is completely filled while preserving the original aspect ratio. If the ratios do not match, some part of the image is cropped.
.blog-card img {
width: 100%;
height: 220px;
object-fit: cover;
}
This is one of the most useful values for card images, hero images, galleries, and thumbnails. It prevents empty space and keeps the layout clean. The tradeoff is cropping, so the image focus matters.
object-fit: none
The value none keeps the media at its natural size and does not resize it to fit the box. If the media is larger than the box, it can be clipped.
.preview {
width: 250px;
height: 250px;
object-fit: none;
}
This is not common for normal responsive images, but it can be useful when showing a specific crop or pixel-perfect preview.
object-fit: scale-down
The value scale-down behaves like either none or contain, whichever produces a smaller concrete object size. In simple words, the object will shrink if needed but will not scale up unnecessarily.
.logo {
width: 240px;
height: 120px;
object-fit: scale-down;
}
This is useful for logos and icons that should not become blurry by being scaled up too much.
object-position
When object-fit: cover crops an image, object-position controls which part stays visible. The default is center center.
.hero-image {
width: 100%;
height: 420px;
object-fit: cover;
object-position: center top;
}
This keeps the top-center part of the image visible. It is useful for portraits, banners, product photos, and hero sections where the important subject is not exactly in the center.
Object Fit with Aspect Ratio
The aspect-ratio property works very well with object-fit. You can define the shape of the image box with aspect ratio and then use object fit to control how the image fills it.
.card-image {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
}
This creates consistent media boxes across cards. The image height is calculated from the width, and cover keeps the card looking full.
Avatars and Circular Images
Avatars usually need a square box and a circular border radius. object-fit: cover prevents faces from becoming stretched.
.avatar {
width: 72px;
height: 72px;
border-radius: 50%;
object-fit: cover;
}
Without object fit, a wide or tall uploaded image may stretch inside the circle. With cover, the image keeps its proportions and crops naturally.
Object Fit vs Background Size
object-fit is used on actual media elements like img and video. background-size is used on CSS background images. Both can create similar visual results, but they are not the same.
| Need | Better Choice |
|---|---|
| Important content image | img with object-fit |
| Decorative background | background-size |
| SEO image with alt text | img with object-fit |
| Hero decoration | background-size or img depending on content |
If the image is meaningful content, prefer an actual image element with alt text. If it is purely decorative, a background image may be acceptable.
Responsive Image Cards
Object fit is common in responsive cards because cards need consistent image areas even when uploaded images have different dimensions.
.article-card img {
width: 100%;
height: clamp(180px, 25vw, 280px);
object-fit: cover;
object-position: center;
}
The height changes with the viewport but stays inside reasonable limits. This keeps cards balanced across desktop and mobile layouts.
Object Fit with Videos
Object fit also works with videos. This is useful when a video should behave like a cover image inside a hero section, preview card, or background-style media area.
.hero-video {
width: 100%;
height: 520px;
object-fit: cover;
}
The video fills the box while keeping its original proportions. Parts of the video may be cropped, so the important action should stay near the center or the chosen object position.
Focal Point Cropping
When using cover, cropping is expected. The main question is which part of the media should remain visible. A portrait may need the face centered near the top. A product photo may need the product centered. A landscape image may need the horizon line preserved.
.portrait-card img {
width: 100%;
aspect-ratio: 3 / 4;
object-fit: cover;
object-position: 50% 20%;
}
The first value controls horizontal position and the second controls vertical position. Percentages, keywords, and length values can be used. This gives designers more control without editing the actual image file.
Object Fit in Product Cards
Product cards often need different object fit values depending on the type of image. If the product must be fully visible, use contain. If the product image is a lifestyle photo and the card needs a full visual crop, use cover.
| Image Type | Recommended object-fit |
|---|---|
| Product on white background | contain |
| Blog thumbnail | cover |
| Profile avatar | cover |
| Logo | scale-down or contain |
| Decorative hero image | cover |
The correct value depends on the content purpose. Cropping a blog thumbnail is fine. Cropping a circuit diagram, product photo, or code screenshot can hide important information.
Using Object Fit with Lazy Loaded Images
Object fit is often used with lazy loaded images. The layout should reserve the image box before the image loads, otherwise the page may jump. Use width, height, or aspect-ratio to reserve space.
.post-thumbnail {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
display: block;
}
The image has a predictable shape even before the file finishes loading. This improves layout stability and makes card grids feel more polished.
Debugging Object Fit
If object fit does not seem to work, inspect the actual element box. The media may not have a useful height, or the rule may be applied to a wrapper instead of the image itself.
- Confirm the rule is applied to img, video, or another replaced element.
- Check that the element has a width and height or an aspect ratio.
- Use display: block if inline image spacing causes visual gaps.
- Check object-position when important content is cropped.
- Use contain instead of cover when every part must remain visible.
Object Fit Is Not Art Direction
Object fit changes how one media file is placed inside a box. It does not choose a different image for a different screen. If a mobile layout needs a different crop or a different image composition, use responsive image techniques such as picture or different image sources.
<picture>
<source media="(max-width: 700px)" srcset="portrait.jpg">
<img src="wide.jpg" alt="Project preview">
</picture>
This lets the browser choose a more suitable image. After that, object fit can still control how the selected image fits inside the final box. Use both ideas together when the design needs strong control.
Accessibility and Object Fit
Object fit is visual only. It does not replace image accessibility. If the image communicates information, keep useful alt text on the image. If the image is decorative, use empty alt text or a CSS background depending on the situation.
Also avoid cropping important text inside an image. A cropped diagram, table, or screenshot can become useless even if the card layout looks neat. For technical content, readability is more important than perfect thumbnail symmetry.
Use object fit to improve presentation, but keep the actual meaning of the media visible.
That balance matters most in content-heavy websites.
Common Object Fit Mistakes
- Using object-fit without setting a useful width and height.
- Using fill for photos and accidentally stretching them.
- Using cover on product images where cropping hides important details.
- Forgetting object-position when the subject is cropped badly.
- Using background images for meaningful content that needs alt text.
- Expecting object-fit to work on normal div elements.
Object Fit in CSS FAQ
What does object-fit do in CSS?
object-fit controls how an image, video, or other replaced element fits inside its own box.
What is the difference between cover and contain?
cover fills the box and may crop the media, while contain shows the whole media and may leave empty space.
Why is object-fit not working?
Check that the element is a replaced element and that it has a defined box size through width, height, or aspect-ratio.
Can object-fit work with background images?
No. Use background-size for CSS background images.
Continue learning CSS in order
Follow the topic sequence with the previous and next lesson.