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
2
votes
3 answers

Using CSS object-fit:contain on image leaves blank spaces around it

I have a container image-wrapper and an image inside it. The container has a fixed height and width. Inside it, the image resolution will be dynamic. All I am trying to do is to center the image to the container, horizontally and vertically and…
Debsmita Paul
  • 1,442
  • 9
  • 22
2
votes
1 answer

Object-fit does not cover the entire containing area

The image does not cover the containing area completely (you can see that little teal strip at the bottom.) Why is that? and what can I do in so that the image can cover the div completely? div { width: 300px; } .container { width:…
Phantom
  • 423
  • 1
  • 5
  • 13
2
votes
0 answers

Is object-fit:cover the recommended method to setup full screen background video?

I have two methods to setup full screen background video for a website, Below, I have demos for both methods: Method #1: https://codepen.io/irfan-dayan/pen/MqYaMd HTML:
Irfan
  • 4,882
  • 12
  • 52
  • 62
2
votes
1 answer

CSS object fit cover is stretching image

CSS object fit position fill, contain are working but cover works like fill. It has to fill div without stretching no matter how much part of image would be cover. I set div to flex and apply object fit position to images that are inside in div. Why…
Dipak
  • 931
  • 14
  • 33
2
votes
0 answers

what are multiple ways to display a div and an image next to each other and keep the same height responsively?

.container { display: flex } .image { max-width: 100%; height: 100% } .videotext { padding: 50px; color: white; align-text: center; background-color: blue; width: 50%; max-width: 100%; } img { width:…
nicedood
  • 53
  • 1
  • 8
2
votes
2 answers

Strange gap at the bottom of the HTML video element, when using object-fit: cover;

In some cases, when one of the parent elements in HTML is having margin of padding defined at the bottom or at the top, it will happen that your video object can't stretch the source media to the fullest within itself. Even if you define the…
Mladen Janjetovic
  • 13,844
  • 8
  • 72
  • 82
2
votes
1 answer

How to align text to image that is auto scaled with object-fit: contain

I have a viewport that takes up about 75% of the screen and I fit single images of varying aspect ratios into it using the object-fit: contain css. Above the image I have text labels for the file name & type. I want to keep these aligned to start at…
Adam James
  • 3,833
  • 6
  • 28
  • 47
1
vote
0 answers

An "object-fit" bug in Safari - only occurs for certain images

I recently took over a Wordpress site which has a lot of images. There was a folder of 10Gbs+ worth of PNG files and in an attempt to optimize the site I wanted to convert these to JPG files. I batch converted the whole lot, and then built a process…
Ben Jackson
  • 529
  • 4
  • 12
1
vote
1 answer

mouseMove function not mapping properly when object-fit:contain used in CSS

I'm using code that allows a user to select a color from an image when the mouse hovers that pixel. When using object-fit:contain for the image the colors do not map correctly. The script is acting as if the image is stretched to 100% width / 100%…
N Trav
  • 11
  • 2
1
vote
1 answer

SVG height not behaving as expected

Not that it matters but I'm using https://getwaves.io/ to generate an SVG image. What should happen The blue waves should take up much more vertical space in the gray box. It seems like it does not use object-fit: cover. In fact, it should follow…
Jens Törnell
  • 23,180
  • 45
  • 124
  • 206
1
vote
0 answers

Extra whitespace on top of image in Safari

Can anybody explain why some iphones running safari have extra space on top of the image? I have included an example project here: https://jsfiddle.net/3wL9m6qf/16/

1
vote
1 answer

Image jumps during page load object-fit: cover

For example, here https://cs.daihatsu-club.net/vozidlo/daihatsu-materia-8 when I set Network throttling to Slow 3G in Chrome Developer Tools, you can see how the wedding photo loads and displays first by filling the 300x200px reserved for IMG at the…
Pavel
  • 25
  • 1
  • 8