CSS Object-fit


🔹 What is CSS object-fit?

The CSS object-fit property specifies how the content of a replaced element (like <img>, <video>, or <iframe>) should be resized to fit its container box.

It controls how the image or video scales and crops to fill the space without distorting the aspect ratio.


🔸 Values of object-fit

Value Description
fill Stretches the content to fill the container, ignoring aspect ratio (may distort).
contain Scales content to fit inside container while maintaining aspect ratio (may leave empty space).
cover Scales content to cover the entire container while maintaining aspect ratio (may crop).
none Keeps original size of content, ignoring container size.
scale-down Scales content down to the smaller of none or contain.

🔸 Example Usage

img {
  width: 300px;
  height: 200px;
  object-fit: cover;
}

This ensures the image covers the container area without distortion, cropping if needed.


🔸 Visual Differences

  • fill: May stretch image, distorting it.

  • contain: Shows the whole image with letterboxing (empty space).

  • cover: Fills container, cropping overflow.

  • none: Shows image at original size, ignoring container.

  • scale-down: Like none or contain, whichever is smaller.


Practice Questions

Q1. Use object-fit: cover for a 200x200px image container.

Q2. Use object-fit: contain to show full image inside fixed size div.

Q3. Prevent image distortion using object-fit.

Q4. Use object-fit: fill to stretch image to container size.

Q5. Apply object-fit: none to display image at original size.

Q6. Use scale-down to fit image in container but keep original size if smaller.

Q7. Combine object-fit with fixed height and width on images.

Q8. Create circular images with object-fit: cover.

Q9. Use object-fit for responsive video thumbnails.

Q10. Use object-fit in a flexbox container with fixed image size.


Go Back Top