Object Fit — CSS

object-fit decides how an <img> or <video> fills a box whose shape differs from the image. fill (the default) stretches the image to match the box, so it can…

Sometimes an image gets a size with a different shape from the original photo, like a wide photo put into a square box. The browser then has to choose one of three ways: squash the image (its shape changes), cut off its edges, or leave some empty space. object-fit is the property for <img> (and <video>) that decides which way is used.

An image in a frame

This example puts a wide photo, 800 × 400px, into a square frame:

<div class="frame">
  <img src="https://picsum.photos/800/400" alt="Photo">
</div>

<style>
.frame {
  width: 200px;
  height: 200px;
}
.frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

The result: the frame is completely filled and the photo keeps its shape, but the left and right sides of the photo are gone. Notice: object-fit goes in the rule for the <img>, not in the frame's rule.

fill, cover and contain

object-fit has three values you will use a lot:

So an 800 × 400px photo given width: 200px and height: 200px with no object-fit gets squashed. The browser uses fill, so the wide photo is pressed into a square.

Pick cover when the box must be completely filled, for example for a profile picture or a thumbnail (a small preview image). Pick contain when no part of the image may be cut off, for example for a product photo or a logo.

Which part to keep: object-position

This rule adds object-position to an image that uses cover:

.frame img {
  object-fit: cover;
  object-position: top;
}

By default, cover keeps the middle of the image and cuts the same amount off both edges. object-position decides which part of the image is kept. top keeps the top part when the image is cut at the top and bottom, which suits photos of people so their faces aren't cut off. Other values: bottom, left, right and center.

⚠️ Why doesn't the image change?

TL;DR: object-fit decides how an <img> or <video> fills a box whose shape differs from the image. fill (the default) stretches the image to match the box, so it can get squashed. cover fills the whole box and cuts off the extra edges. contain shows the whole image and leaves empty space. Write object-fit in the image's rule, together with width and height. object-position picks which part of the image is kept when cover cuts.

⚡ Fill the frame

Write:

.frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Meaning: .frame is a frame (a wrapping box), and the <img> inside it is made the same size as the frame. object-fit decides how an image fills a box whose shape differs from the image. The value cover makes the image cover the whole box without squashing it (its shape doesn't change), and the extra edges are cut off. Write object-fit in the <img> rule, not in the frame's rule.

⚡ fill, cover, contain

Guess first: an 800 × 400px photo gets a 200 × 200px size with no object-fit. What happens?

It gets squashed. The default value of object-fit is fill, so the wide photo is pressed into a square.

Meaning: fill stretches the image to match the box, so it can get squashed. cover fills the whole box but cuts off the image's edges. contain shows the whole image and leaves empty space. cover suits a profile picture, contain suits a logo.

⚡ The part that's kept

Write:

.frame img {
  object-fit: cover;
  object-position: top;
}

Meaning: By default, cover keeps the middle of the image and cuts the same amount off both edges. object-position: top keeps the top part, so faces in photos of people aren't cut off.

What you will learn