CSS Object Fit

CSS Object Fit controls how an image or video fits inside its container. It helps maintain the aspect ratio while resizing media elements.

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;

}
The 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
Use 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
Use 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
Use 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;

}
Different 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
CSS Object Fit helps create professional-looking image layouts with minimal CSS.

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;

}
Choose the appropriate 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 cover for profile pictures and image galleries.
  • Use contain when the entire image must remain visible.
  • Avoid using fill if image distortion is unacceptable.
  • Specify both width and height for predictable results.
  • Combine object-fit with object-position for better alignment.
  • Test images on different screen sizes.
  • Optimize image size for faster page loading.
  • Use responsive layouts whenever possible.
Proper use of 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;

}
Choosing the wrong 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
Modern browsers provide excellent support for the object-fit property.

14. CSS Object Fit Interview Questions

  1. What is the purpose of the object-fit property?
  2. What is the difference between cover and contain?
  3. Which value may distort an image?
  4. What does the none value do?
  5. How does scale-down work?
  6. Can object-fit be used with videos?
  7. What is the default value of object-fit?
  8. How is object-fit different from background-size?
  9. Which value is best for profile pictures?
  10. Is object-fit supported in modern browsers?
Practice these interview questions to strengthen your understanding of CSS Object Fit.

15. Lesson Summary

In this lesson, you learned:
  • ✔ Introduction to CSS Object Fit.
  • ✔ The object-fit property.
  • fill value.
  • contain value.
  • cover value.
  • none value.
  • scale-down value.
  • ✔ Practical examples.
  • ✔ Best Practices and Common Mistakes.
  • ✔ Browser Support and Interview Questions.
Congratulations! You have successfully completed the CSS Object Fit lesson. You can now display images and videos beautifully using the appropriate object-fit values for responsive web design.

Quick Quiz

Which CSS property controls how an image or video fits inside its container?