Questions tagged [object-fit]

The object-fit CSS property specifies how the contents of a replaced element should be fitted to the box established by its used height and width.

The object-fit CSS property specifies how the contents of a replaced element should be fitted to the box established by its used height and width.

Syntax

The object-fit property is specified as a single keyword chosen from the list of values below.

Values

  • fill (Initial value) - The replaced content is sized to fill the element’s content box: the object’s concrete object size is the element’s used width and height.

  • contain - The replaced content is sized to maintain its aspect ratio while fitting within the element’s content box: its concrete object size is resolved as a contain constraint against the element’s used width and height.

  • cover - The replaced content is sized to maintain its aspect ratio while filling the element’s entire content box: its concrete object size is resolved as a cover constraint against the element’s used width and height.

  • none - The replaced content is not resized to fit inside the element’s content box: the object’s concrete object size is determined using the default sizing algorithm with no specified size, and a default object size equal to the replaced element’s used width and height.

  • scale-down - The content is sized as if none or contain were specified, whichever would result in a smaller concrete object size.

References

Browser Support

113 questions
29
votes
2 answers

Using object-fit on a

I have an outer container that is variable in size and width. Suppose that inside this container, I have a canvas that I want to have grow as much as it can while maintaining proportion and not cropping. For this, I would normally use object-fit:…
Brad
  • 159,648
  • 54
  • 349
  • 530
25
votes
5 answers

Object-fit: Cover and Srcset

I am using object-fit: cover on a bunch of images. The frame for the image takes up 50vw and has a dynamic height. The cover attribute works great, but it means that I don't really know how wide my actual image will be at a given time. Most likely…
Andrew Whited
  • 281
  • 3
  • 6
23
votes
5 answers

How to use objectFit for Next.js 13 ?

In the next/image docs under version history, it states the following for v13.0.0: layout, objectFit, objectPosition, lazyBoundary, lazyRoot props removed. Under next/image docs for fill the documentation states we can set the object fit with…
MGamboa
  • 355
  • 1
  • 3
  • 6
15
votes
2 answers

How to get unknown style rule in MS Edge with JavaScript

I want to use object-fit CSS rule. This is not supported in MSIE and MS Edge Browser. While there are some polyfills for IE, there is none to my knowledge that works in Edge. E.g. the polyfill fitie by Jonathan Neal works with IE, but not in Edge…
yunzen
  • 32,854
  • 11
  • 73
  • 106
13
votes
2 answers

object-fit: cover causes video to jump in safari

I'm working on a simple example that utilizes the CSS object-fit: cover property on a video element that is wrapped inside a div with specified width and aspect ratio. HTML
Zampus
  • 143
  • 5
11
votes
2 answers

CSS object-fit: contain; is keeping original image width in layout

I'm trying to make my images responsive inside some flexbox containers using object-fit: contain, and while the images do resize, the layout seems to be keeping the original image size, causing a scrollbar to appear. Checking the width of the image…
dangel
  • 7,238
  • 7
  • 48
  • 74
9
votes
1 answer

CSS object-fit cover is stretching a particular image in Chrome on Mac

img { width: 200px; height: 150px; object-fit: cover; } Codepen Example Many other images work fine. In other browsers everything also…
dmtrbrl
  • 101
  • 4
8
votes
3 answers

Aligning image height along a row

Struggling to get things to behave. I have a dynamic list of images on a responsive site. The layout is generated where images can be in rows/columns or columns rows. This example is close but the paired images don't align at the bottom as the…
Dan
  • 367
  • 1
  • 5
  • 17
8
votes
1 answer

Contain image size with parent

I´ve been trying to keep my images next to each other on the same line, and just crop them to a smaller size if needed. Why doesn't object-fit work ? HTML:
7
votes
1 answer

How does object-fit work with canvas element?

I have been unable to find any documentation to tell me one way or another. Am I able to use object-fit cover on a canvas elements? I have done some experimenting and it is not behaving as expected. Can somebody give me a definitive answer?
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
7
votes
1 answer

Image with object-fit not filling container

I have a banner image that's wider than it is tall. I have a container div that displays different sizes of images, and for the banner style, I need it to stretch and/or squash to fill the parent div in both directions. I've looked at…
EmmyS
  • 11,892
  • 48
  • 101
  • 156
7
votes
2 answers

Achieving an Overlapping image in a flex container with object-fit

Hey there i have asked a similar question before and was able to achieve it , the thing is that i had to use a png image, the downside is that its way too big. After some research i found a method using a svg container and a alpha and beta channel…
HendrikEng
  • 654
  • 1
  • 10
  • 32
5
votes
1 answer

Click event outside image when using object-fit on img

I suppose it's not possible, but want to double check: If I have an image that is object-fit:contain; and then click outside the image (on the background) but still inside the img frame - is there a way to let the click event bypass the img, just…
joe_g
  • 685
  • 1
  • 6
  • 17
5
votes
1 answer

Is object-fit breaking some image aspect ratios in Chrome?

I'm using object-fit:cover on some IMG tags within a wrapper div to try to fill a div with the image. I've noticed that some files seem to have their aspect ratios stretched, while others are displayed correclty. This occurs specifically in Chrome…
Mark
  • 3,459
  • 1
  • 18
  • 23
5
votes
0 answers

How to apply object-fit: contain also to div?

There is object-fit: contain as a way to resize the image so that the entire image fits within the frame, aligning the long side of the image with the frame. h2 { font-family: Courier New, monospace; font-size: 1em; margin: 1em 0…
user11091222
1
2 3 4 5 6 7 8