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
1
vote
1 answer

Apply object-fit to an element with Typescript in Angular 2

Im trying to use object-fit for an element in my app. My IDE doesnt recognize this: child.style.objectFit = 'cover'; For some reason. It has squigly lines under objectFit. And when i do this: child.style.setProperty( 'object-fit', 'cover',…
Martijn van den Bergh
  • 1,434
  • 1
  • 20
  • 40
0
votes
0 answers

Border radius not working for img tag with width property

I need to make round corners for image in div. Image shoul contain this div. Here is my realisation of that: .img-module { display: flex; justify-content: center; position: relative; overflow: hidden; height: 70vh; grid-column: 1/2; …
AlexCap
  • 9
  • 2
0
votes
0 answers

How do i simulate object-fit: fill with fluent-ffmpeg nodejs

ffmpeg(input) .output(output) .outputOptions("-vf",scale=w=${1920}:h=${1080}) .outputOptions("-c:a", "copy") .on("end", () => { console.log("Video conversion completed"); }) .on("error", error => { console.error("Video conversion failed:", error);…
0
votes
1 answer

Prevent image forcing height of container inside flexbox parent

I have a tricky layout to solve with a grid of images, the first row runs as 4 blocks of 25% across, the second will have 3 blocks with 2 x 25% blocks and 1 image taking up 50% of the remaining space. I have the layout working fine I just need to…
0
votes
2 answers

Flexbox Object-fit Image

I created a very simple flexbox layout, I have two rows and 5 columns. My idea is to put pictures in the boxes, but when I add an image it distorts the grid. My code looks like:
0
votes
0 answers

How to use the Next.js Image component to fill parent's container height AND let the width overflow the parent container?

I am trying to set a fancy responsive alternate layout in next.js. The layout is 38%/62% width-wise and I need an image in the 38% part (left) to overflow under another image on the 62% part (right). I have tried setting absolute properties but this…
0
votes
1 answer

How do I make a canvas fill a div while keeping it's aspect ratio?

I have a canvas element inside a flexbox that may be resized. How do I get the canvas to fill the available space without losing its aspect ratio (2/3), and without being cut off? (I want to change the CSS dimensions, not the canvas…
Chris
  • 358
  • 1
  • 14
0
votes
1 answer

Are different object-position values for source elements possible

Is it possible to set different object position properties for different sources within a picture element. For example, the code below doesn't work:
Kingsford
  • 15
  • 3
0
votes
0 answers

How to fit an image inside a fixed size div

I have a div containing two divs: One for image(30%) and one for text(70%) Inside the image div, i am fitting my img. I want the img(image-container) inside the image div(img-banner) to:- Not exceed outside the div, it should fit in Should not be…
0
votes
1 answer

Why does object-fit impact my image quality and how to avoid it

Using object-fit: cover has a bad impact on my image quality. We can see that when the element uses object-fit: cover the hair in the image gets pixelated. Here's a demo of my code: https://codepen.io/widness/pen/NWBywgy I don't understand why the…
Raphaël Balet
  • 6,334
  • 6
  • 41
  • 78
0
votes
1 answer

Resize background-image to flexbox

I want the background-image in the "main" div to fit into the flexbox, without changing the picture aspect ratio. This is what I wrote in HTML/CSS: .main { display: flex; width: 100%; height: 853px; object-fit: cover; object-position:…
Lehe
  • 1
  • 2
0
votes
1 answer

Show image using `object-fit: cover`, but only if it doesn't fit the container

I want to show images on my website with the following logic: If the image fits within the maximum size of the container (240 pixels tall, 100% width), just show it as-is Otherwise show it using the object-fit: cover style So for huge images, they…
Kevin Renskers
  • 5,156
  • 4
  • 47
  • 95
0
votes
0 answers

getBoundingClientRect() returns "top: 0" and "left: 0" when element has "object-fit: contain; width: 100%; height: 100%;"

I have just a HTML canvas element which I want to center on screen and fit to screen while maintaining aspect-ratio and not cropping, so black bars will show either horizontally or vertically if the device aspect ratio isn't equal to the canvas…
0
votes
1 answer

White space between IMG and parent DIV while using object-position

I have a card with image, text and overlay gradient. The image is much bigger than its parent div. The image is made responsive to the size of parent div. I need to position the image inside div a certain way, so I use object-position for…
Ana
  • 27
  • 6
0
votes
1 answer

Alternative to object-fit with wkhtmltopdf

I am currently trying to export html pages to pdf using wkhtmltopdf, but the object-fit I use to force my images into a square doesn't work; the images end up stretched to fit the square box instead of being cropped properly. I know for a fact that…
Rilves
  • 146
  • 7