CSS Object Fit
1. Introduction to CSS Object Fit
The object-fit property specifies how the content of an image or video should be resized to fit its container. It is especially useful for responsive web design and image galleries.
Why Use object-fit?
- Maintains image proportions.
- Creates responsive image layouts.
- Prevents image distortion.
- Works with fixed-size containers.
- Improves gallery and card layouts.
img{
width:300px;
height:200px;
object-fit:cover;
}
object-fit property helps images and videos
display beautifully inside containers.
2. object-fit Property
The object-fit property determines how the content should fill its container while preserving or ignoring its aspect ratio.
img{
object-fit:cover;
}
| Value | Description |
|---|---|
| fill | Fills the container (may distort image). |
| contain | Fits entire image inside container. |
| cover | Fills container while maintaining aspect ratio. |
| none | Displays original image size. |
| scale-down | Uses the smaller result of none or contain. |
cover is the most commonly used value for responsive image cards.
3. fill Value
The fill value stretches the image to completely
fill the container. This is the default value and may distort
the image's aspect ratio.
img{
width:300px;
height:200px;
object-fit:fill;
}
| Feature | Behavior |
|---|---|
| Aspect Ratio | May change |
| Container Filled | ✔ Yes |
fill only when image distortion is acceptable.
4. contain Value
The contain value resizes the image so the
entire image fits inside the container while maintaining
its aspect ratio.
img{
width:300px;
height:200px;
object-fit:contain;
}
| Feature | Behavior |
|---|---|
| Aspect Ratio | Maintained |
| Entire Image Visible | ✔ Yes |
| Empty Space Possible | ✔ Yes |
contain when the complete image must remain visible.
5. cover Value
The cover value scales the image to completely
cover the container while preserving its aspect ratio.
Some portions of the image may be cropped.
img{
width:300px;
height:200px;
object-fit:cover;
}
.gallery img{
object-fit:cover;
}
| Feature | Behavior |
|---|---|
| Aspect Ratio | Maintained |
| Container Filled | ✔ Yes |
| Image Cropping | Possible |
cover is ideal for profile pictures, cards,
banners, and image galleries.
6. none Value
The none value displays the image in its
original size without resizing it to fit the container.
If the image is larger than the container, some parts may
overflow.
img{
width:300px;
height:200px;
object-fit:none;
}
| Feature | Behavior |
|---|---|
| Image Resized | ❌ No |
| Original Size | ✔ Yes |
| Overflow Possible | ✔ Yes |
none when you want to display media at its original dimensions.
7. scale-down Value
The scale-down value compares the results of
none and contain and uses
whichever produces the smaller image.
img{
width:300px;
height:200px;
object-fit:scale-down;
}
| Value | Behavior |
|---|---|
| none | Uses original image size. |
| contain | Fits image inside the container. |
| scale-down | Chooses the smaller result. |
scale-down automatically prevents unnecessarily enlarging images.
8. Practical Object-Fit Examples
Example 1 : Profile Picture
.profile{
width:150px;
height:150px;
object-fit:cover;
}
Example 2 : Product Image
.product{
width:250px;
height:200px;
object-fit:contain;
}
Example 3 : Banner Image
.banner{
width:100%;
height:300px;
object-fit:cover;
}
Example 4 : Thumbnail
.thumbnail{
width:120px;
height:120px;
object-fit:fill;
}
object-fit values are useful for profile pictures, banners, galleries, and product images.
9. Advantages of CSS Object Fit
- Maintains image quality.
- Creates responsive image layouts.
- Reduces image distortion.
- Perfect for image galleries.
- Works well with videos.
- Easy to use and maintain.
- Improves user experience.
| Feature | Available |
|---|---|
| Responsive Images | ✔ Yes |
| Aspect Ratio Support | ✔ Yes |
| Easy to Implement | ✔ Yes |
10. CSS Object Fit Comparison Table
| Value | Aspect Ratio | Container Filled |
|---|---|---|
| fill | ❌ No | ✔ Yes |
| contain | ✔ Yes | ❌ Not Always |
| cover | ✔ Yes | ✔ Yes |
| none | ✔ Original | ❌ No |
| scale-down | ✔ Yes | Depends on Image Size |
.gallery img{
width:250px;
height:180px;
object-fit:cover;
}
object-fit value based on whether you want to preserve the entire image, fill the container, or display the original size.
11. CSS Object Fit Best Practices
Following best practices helps create responsive and visually appealing image and video layouts.
- Use
coverfor profile pictures and image galleries. - Use
containwhen the entire image must remain visible. - Avoid using
fillif image distortion is unacceptable. - Specify both
widthandheightfor predictable results. - Combine
object-fitwithobject-positionfor better alignment. - Test images on different screen sizes.
- Optimize image size for faster page loading.
- Use responsive layouts whenever possible.
object-fit creates clean and professional image layouts.
12. Common CSS Object Fit Mistakes
| Mistake | Correct Practice |
|---|---|
Using fill for every image. |
Choose the appropriate value for each use case. |
| Not specifying image dimensions. | Always define width and height. |
| Ignoring image cropping. | Use contain if cropping is not acceptable. |
| Using very large images. | Optimize images before uploading. |
| Not testing responsiveness. | Check layouts on different devices. |
❌ Poor
img{
object-fit:fill;
}
✔ Better
img{
object-fit:cover;
}
object-fit value can reduce image quality and user experience.
13. Browser Support
The object-fit property is supported by all
major modern web browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
object-fit property.
14. CSS Object Fit Interview Questions
- What is the purpose of the
object-fitproperty? - What is the difference between
coverandcontain? - Which value may distort an image?
- What does the
nonevalue do? - How does
scale-downwork? - Can
object-fitbe used with videos? - What is the default value of
object-fit? - How is
object-fitdifferent frombackground-size? - Which value is best for profile pictures?
- Is
object-fitsupported in modern browsers?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Object Fit.
- ✔ The
object-fitproperty. - ✔
fillvalue. - ✔
containvalue. - ✔
covervalue. - ✔
nonevalue. - ✔
scale-downvalue. - ✔ Practical examples.
- ✔ Best Practices and Common Mistakes.
- ✔ Browser Support and Interview Questions.
object-fit values for responsive web design.
Quick Quiz
Which CSS property controls how an image or video fits inside its container?