🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Object-fit

AI & DATA SCIENCE // object-fit

The object-fit property controls how the content of a replaced element, like an <img> or <video>, is resized and cropped to fit its own specified box dimensions, working similarly to background-size but for actual embedded content.

Syntax

object-fit: fill | contain | cover | none | scale-down;

Deep Dive Course

fill, the default, stretches the content to completely fill its box, distorting its aspect ratio if the box's proportions don't match the content's natural ones — cover scales the content, preserving its aspect ratio, to completely fill the box, cropping any excess, exactly like background-size: cover; contain instead scales it to fit entirely within the box without cropping, potentially leaving empty space, exactly like background-size: contain; and none displays the content at its natural size regardless of the box's dimensions, potentially overflowing or leaving empty space around it. This is specifically for actual embedded content elements like <img> and <video>, distinct from background-size, which serves the equivalent purpose for CSS background images instead.

1Understanding Object-fit

fill, the default, stretches the content to completely fill its box, distorting its aspect ratio if the box's proportions don't match the content's natural ones — cover scales the content, preserving its aspect ratio, to completely fill the box, cropping any excess, exactly like background-size: cover; contain instead scales it to fit entirely within the box without cropping, potentially leaving empty space, exactly like background-size: contain; and none displays the content at its natural size regardless of the box's dimensions, potentially overflowing or leaving empty space around it. This is specifically for actual embedded content elements like <img> and <video>, distinct from background-size, which serves the equivalent purpose for CSS background images instead.

💡

Use object-fit: cover on an <img> element specifically when you need it to completely fill a fixed-size box without distorting its aspect ratio, cropping any excess instead — this avoids the default fill behavior's tendency to visually squash or stretch images that don't naturally match the box's exact proportions.

editor.html
img {
  width: 300px;
  height: 300px;
  object-fit: cover;
}
localhost:3000

2Practical Example

Here is a real-world application of Object-fit showing how it is used in production CSS code.

editor.html
img {
  width: 300px;
  height: 300px;
  object-fit: contain;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Object-fit:

1. Use object-fit: cover for <img>/<video> elements that need to completely fill a fixed-size box while preserving their natural aspect ratio, cropping any excess

2. Avoid relying on the default fill value when an image's natural aspect ratio doesn't closely match its container's box, since fill distorts and stretches the image to force it to fit exactly

3. Pair object-fit with object-position, which works like background-position, to control which part of the content remains visible when cropping occurs under object-fit: cover

⚠️

Tip: Use object-fit: cover on an <img> element specifically when you need it to completely fill a fixed-size box without distorting its aspect ratio, cropping any excess instead — this avoids the default fill behavior's tendency to visually squash or stretch images that don't naturally match the box's exact proportions.

editor.html
img {
  width: 300px;
  height: 300px;
  object-fit: cover;
}
localhost:3000

Examples

Example 01Basic Usage
img {
  width: 300px;
  height: 300px;
  object-fit: cover;
}
Example 02Advanced Example
img {
  width: 300px;
  height: 300px;
  object-fit: contain;
}

Best Practices

  • Use object-fit: cover for /
  • Avoid relying on the default fill value when an image's natural aspect ratio doesn't closely match its container's box, since fill distorts and stretches the image to force it to fit exactly
  • Pair object-fit with object-position, which works like background-position, to control which part of the content remains visible when cropping occurs under object-fit: cover

Interview Question

Why does the default object-fit value, fill, often visually distort or stretch an image, and why doesn't this happen when object-fit: cover is used instead?

Hint: Think about whether each value preserves the image's own natural aspect ratio, or simply stretches it independently along each axis to exactly match the box's dimensions.

fill, the default value, forces the image's width and height to stretch or shrink independently along each axis until they exactly match the specified box's width and height precisely, with absolutely no regard at all for whether that forced resizing preserves the image's own natural aspect ratio, the original proportional relationship between its width and height — if the box's proportions differ at all from the image's natural proportions, this independent per-axis stretching necessarily distorts the image, visually squashing or stretching it out of its natural shape to force an exact fit. cover, by contrast, explicitly scales the image uniformly, preserving its natural aspect ratio throughout, meaning both its width and height are scaled by the exact same proportional factor together rather than independently, and instead of distorting the image to force an exact fit, it scales the image up just enough to completely cover the box's full area while cropping away whatever excess extends beyond the box's boundaries. This fundamental difference, independent non-uniform stretching that distorts shape versus uniform scaling that preserves shape while cropping excess instead, is exactly why fill so often produces a visually distorted, squashed-looking result while cover reliably preserves the image's natural, undistorted appearance.

Exercises

MediumPractice using Object-fit in a real scenario.
View Solution
img {
  width: 300px;
  height: 300px;
  object-fit: cover;
}

Frequently Asked Questions

Why does the default object-fit value, fill, often visually distort or stretch an image, and why doesn't this happen when object-fit: cover is used instead?

fill, the default value, forces the image's width and height to stretch or shrink independently along each axis until they exactly match the specified box's width and height precisely, with absolutely no regard at all for whether that forced resizing preserves the image's own natural aspect ratio, the original proportional relationship between its width and height — if the box's proportions differ at all from the image's natural proportions, this independent per-axis stretching necessarily distorts the image, visually squashing or stretching it out of its natural shape to force an exact fit. cover, by contrast, explicitly scales the image uniformly, preserving its natural aspect ratio throughout, meaning both its width and height are scaled by the exact same proportional factor together rather than independently, and instead of distorting the image to force an exact fit, it scales the image up just enough to completely cover the box's full area while cropping away whatever excess extends beyond the box's boundaries. This fundamental difference, independent non-uniform stretching that distorts shape versus uniform scaling that preserves shape while cropping excess instead, is exactly why fill so often produces a visually distorted, squashed-looking result while cover reliably preserves the image's natural, undistorted appearance.

Related Functions

Background-sizeRefDimensionsBackground-position