# object-fit

> The `object-fit` CSS property specifies how an element's content, such as images or videos, should fit within its container.

## How do I make an image fill a box without stretching in CSS?

The property that specifies how an element's content should fit within its container. It works with replaced elements like images and videos, enabling to set the content to 'contain', 'cover', 'fill', 'scale-down' or 'none', adjusting how it scales and fits.

- Topic: Image Handling
- Type: property
- Browser support: Chrome 32+, Chrome Android 32+, Edge 79+, Firefox 36+, Firefox Android 36+, Safari 10+, Safari iOS 10+

## Example

Wrong:

```css
.card img {
  width: 100%;
  height: 200px;
}
```

Right:

```css
.card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}
```

`object-fit: cover` crops the image to fill the box instead of stretching it to the new proportions.

## Common mistakes

- The image is stretched because only `width` is set and `object-fit` has nothing to fit into.
- Using `object-fit` on a `div` with a background image and nothing changes.

## Key points

- Images with `object-fit` have both a `width` and a `height` set.
- Cropped images use `object-fit: cover`; images that must be fully visible use `contain`.
- Background images use `background-size`, since `object-fit` only works on replaced elements like `img` and `video`.

## Interview questions

### What is the difference between object-fit cover and contain?

`cover` fills the whole box and crops the overflow. `contain` shows the whole image and leaves empty space.

### Does object-fit work on background images?

No. It works on replaced elements like `img` and `video`; for backgrounds use `background-size: cover`.

## Related cards

- [object-position](https://wonderium.dev/css/object-position.md)
- [aspect-ratio](https://wonderium.dev/css/aspect-ratio.md)
- [background-size](https://wonderium.dev/css/background-size.md)
- [`<img>`](https://wonderium.dev/html/img.md)
- [`<video>`](https://wonderium.dev/html/video.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/object-fit
- Last updated: 2026-11-25
