日本語

Why object-fit Doesn’t Work in CSS and How to Fix It

​​CSS​HTML,Playwright

object-fit works on <img> and <video> elements themselves, and only when you set both the width and the height of that element. If you put it on an outer <div>, or leave the img height as auto, cover does nothing and the image is not cropped. This article covers why images in product list thumbnails, card images, or profile icons look squashed or stay wide when you want them to be the same shape, and how to fix that.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, Playwright 1.63.0, and Chromium 158. I opened a page with only HTML and CSS in Playwright, then counted how many pixels each color continued for along the middle horizontal row and the middle vertical column of the box in the screenshot. I did not test Firefox, Safari, or <video> elements.

Three panels stacked vertically. The top panel, "object-fit: cover on the outer div" (labels in Japanese), shows a white circle squashed into a tall ellipse, with all four bands A to D visible. The middle panel, "img left at width: 100%; height: auto" (labels in Japanese), shows a wide box with all four bands visible. The bottom panel, "img with a set height and object-fit: cover" (labels in Japanese), shows the circle in its normal shape inside a square box, with only bands B and C visible
Chromium rendering in a 240px-wide box: the two ways that do not work for object-fit: cover (top and middle), and the way that does work (bottom)

Check the result by where the image is cropped

To check the effect, I prepared an image 400px wide and 200px tall. From left to right it has red, yellow, green, and blue bands, each 100px wide. I call them A, B, C, and D. I displayed this image in an img that was 200px wide and 200px tall, changed the object-fit value, and counted the colors visible along the middle horizontal row and the middle vertical column.

object-fit Middle horizontal row Middle vertical column
fill (initial value) A, B, C, and D at 50px each The image is 200px
contain A, B, C, and D at 50px each 50px background above and below, image is 100px
cover B and C at 100px each The image is 200px

With fill, the wide image was squeezed into a square, so each band shrank to 50px. With cover, the image keeps its aspect ratio and fills the box, so A and D on the sides overflowed and were cut off, leaving only B and C in the middle. This matches the behavior described in the MDN object-fit page.

In the sections below, I treat object-fit: cover as "not working" if all four bands A to D are still visible after I set it.

The property is on an outer element, not the img

object-fit only applies to replaced elements such as images and videos. In the property table of the CSS Images 3 specification, the applies-to entry is "replaced elements". Putting it on a <div> or <picture> does nothing to the image inside.

I put it on an outer div

I set object-fit: cover on a <div> that was 200px tall, and gave the img inside width: 100%; height: 100%. The horizontal row showed A, B, C, and D at 50px each. As with fill, the image was squeezed.

getComputedStyle() showed that the <div> had object-fit: cover, but the img still had the initial value fill. object-fit is not an inherited property (the property table in the CSS Images 3 specification says "Inherited: no"), so a value on the parent does not pass down to the child img.

css
.thumb {
  width: 200px;
  height: 200px;
}
.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover; /* put it on the img */
}

After I moved the property to the img, B and C were 100px each, and the image was cropped.

I put it on a picture element

The same thing happens with <picture> when you use it to switch images. With object-fit: cover on the <picture>, all four bands A to D stayed visible, and the object-fit on the img inside was fill. It worked when I put it on the img inside, as in picture img. The MDN picture element page also notes that object-fit should be used on the child <img>, not on <picture>.

For background images, use background-size

I put object-fit: cover on a <div> that shows its image as a background. The computed value became cover, but the display did not change. The image stayed at its original size, placed from the left edge, and bands A and B were visible at 100px each.

To crop a background image to fill the box, use background-size: cover. With the position set to center, B and C were 100px each, which matches what object-fit: cover on an img gives you.

css
.hero {
  height: 200px;
  background: url(photo.jpg) no-repeat center / cover;
}

As described on the MDN background-size page, cover scales the image, keeping its aspect ratio, to the smallest size that fills the element, and then cuts off the overflow at the top and bottom or the left and right. I covered when to use cover and when to use contain in a separate article, "How to Choose Between cover and contain in CSS background-size".

The img height is still auto

Even when the property is on the img, it does nothing if the height is not set.

Inside a box 200px wide, I set the img to width: 100%; height: auto. I changed object-fit to fill, cover, and contain, but the img was always 200×100, and the visible bands were A, B, C, and D at 50px each. With height: auto, the img takes the same 2:1 shape as the image. Since the box and the image have the same shape, there is nothing to crop and no space to fill.

As stated in the CSS Images 3 specification, object-fit controls how the content fits into a box whose size is set by the element's width and height. If the box has the same shape as the image, no value changes how it looks.

Setting width="400" height="200" as attributes on the img did not help if the CSS still had height: auto. The img stayed 200×100, and all four bands were visible.

To fix this, set the height of the img, or set its shape with aspect-ratio.

css
.thumb img {
  width: 100%;
  aspect-ratio: 1; /* or height: 200px; */
  object-fit: cover;
}

Whether I added height: 200px or aspect-ratio: 1, the img became 200×200, and B and C were visible at 100px each. With aspect-ratio, you can set the shape without writing a pixel height. I covered how to line up images with the same aspect ratio in a separate article, "How to Align Images with the Same Aspect Ratio Using CSS".

The parent has no fixed height for height: 100%

Even if the img has height: 100%, it behaves like height: auto when the parent's height is not set.

I put width: 100%; height: 100%; object-fit: cover on the img and did not set a height on the parent <div>. The computed height of the img was 100px, and all four bands A to D were visible. The CSS Box Sizing 3 specification says that a percentage height on a child whose parent has no definite height (one whose height depends on its content) is treated as auto.

When I set min-height: 200px on the parent, the img was still 100px tall. The box became 200px, but the img only filled the top 100px, and the bottom 100px showed the gray background. min-height does not serve as the base for a child's percentage height.

When I set height: 200px or aspect-ratio: 1 on the parent, the img became 200px tall, and only B and C were visible.

css
.thumb {
  width: 200px;
  aspect-ratio: 1; /* or height: 200px; */
}
.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

I covered why height: 100% does not work and how to stretch a child inside a parent with min-height in a separate article, "Why height: 100% Doesn't Work in CSS and How to Fix It".

A later CSS rule overrides it

If none of the causes above apply, check whether another CSS rule is overwriting object-fit. If .thumb img { object-fit: fill; } comes after .thumb img { object-fit: cover; } with the same specificity, the later rule wins. In my test environment, the computed value of the img became fill, and all four bands were visible.

If getComputedStyle(img).objectFit does not return cover for the img, this is the cause. I covered how to find which rule wins in Chrome DevTools, in a separate article, "How to Find the Rule That Beats a Crossed-Out CSS Declaration in Chrome DevTools".

​Posted by 86