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

how can we get new image size dimension after giving object-fit:contain property to the image tag?

enter image description here In the above image, you can see the original image size and image get shrink after giving the property "object-fit:contain". can we calculate the effective image size after giving "Object-fit:contain" property.
4
votes
2 answers

object-fit: fill; does not working

I wrote this responsive image gallery code with using of flexbox I set background-color style of every flexbox_item to cyan , to show the dimensions of every flexbox_item block : background-color: cyan; Now i want images to fill their blocks , so…
Amir
  • 63
  • 1
  • 4
3
votes
1 answer

How to use object-fit with flex-grow / flex-shrink?

I have two vertically stacked containers (in a fixed size parent container): The height of the bottom one is defined by its (text) content. The top one takes up the remaining height and contains an image. The problem is, that object-fit only works…
AFoeee
  • 731
  • 7
  • 24
3
votes
3 answers

How can I use "object-fit: contain" in CSS and still have a border radius on the image?

I'm trying to show an image in a "lightbox" style so that it will fill the available area on screen, in this case 90% of the width of the page and 70% of the height. Using object-fit: contain; seems to be the de facto way to do that but it's not…
Matt
  • 9,068
  • 12
  • 64
  • 84
3
votes
1 answer

Understanding object-fit CSS property

In this article we can read: The object-fit CSS property sets how the content of a replaced element, such as an or
tonitone120
  • 1,920
  • 3
  • 8
  • 25
3
votes
3 answers

Object-fit not affecting video in Edge browser

I'm using object-fit:cover to play background video on my website (scroll down). Firefox and Chrome are displaying it correctly, but Edge not - although it supports the object-fit property. .sec-content > div { display: flex; …
Gallex
  • 311
  • 3
  • 8
  • 18
3
votes
1 answer

object-fit, object-positioning and absolute positioning

Whilst trying to make an image fit into a rectangle, I came across a weird problem and wondered if anyone knew why these three ways of using object fit act differently: .container { width: 250px; padding-top: 20%; border: 1px solid red; …
Pete
  • 57,112
  • 28
  • 117
  • 166
3
votes
0 answers

Video tag object-fit alternative

I need to implement a video, which it fills up the container. It is required to use pure CSS, instead of using Javascript. I have browsed through the internet, and I concluded that there are 2 pure-CSS approaches to this problem, but they still have…
Tyler Bean
  • 417
  • 1
  • 4
  • 14
3
votes
3 answers

Bootstrap Carousel and tag with object-fit & object-position not working

I have the following carousel:
3
votes
1 answer

set css object-position property programmatically

I am using the object-fit and object-position css properties to control the placement of an image in css. I want to programmatically set that position with javascript. I tried: const panPos = 50; document.querySelector('.image').style.objectPosition…
mheavers
  • 29,530
  • 58
  • 194
  • 315
2
votes
2 answers

Limit height of image to a fixed aspect ratio and use object-fit when it exceeds it

I have an image with some text in a responsive column. The image is chosen by users so I don't know it's aspect ratio beforehand. I want to limit the height of the image to an aspect ratio of 1 / 1. This means : Landscape images are shown…
web-tiki
  • 99,765
  • 32
  • 217
  • 249
2
votes
0 answers

"object-fit: cover;" on Chrome renders images jagged

I am using object-fit: cover; to display an image inside a square (or circle) while maintaining the original proportions of the image. The only problem that I've noticed is that in Chrome if the image is taller than it is wide, it is shown as…
Hexile
  • 65
  • 1
  • 8
2
votes
2 answers

Make image fit in a container, without object-fit: contain

The following code is a perfect solution to make an image: fit in a given container be centered horizontally and vertically in the container use the maximum possible size without changing the aspect ratio Perfect, except that the library…
Basj
  • 41,386
  • 99
  • 383
  • 673
2
votes
6 answers

How can I set the max-height same as width

I'm currently working on my blog where I am trying to fit all Images into a 1:1 Ratio, which works great for images where the height is larger than the width. Well on the other hand it also works "well" with images where the width is bigger. But the…
brox
  • 53
  • 7
2
votes
1 answer

Why doesn't `width:100%; height:100%; object-fit: contain;` make a

So I have a page with a grid layout, with a header and a footer and a black content container in the middle. html, body { height: 100%; margin: 0; padding: 0; } .container { display: grid; height: 100%; grid-template-rows: max-content…
Peter Olson
  • 139,199
  • 49
  • 202
  • 242