Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive object-fit: cover fix on Chrome

Tags:

I know there's others questions about this, but they are old and not updated with the browsers current support. And they not cover the Chrome particular problem with this.

I want a video (I want to do that with image too, but here I'm using a video) occupying 100% the width of the window, but with the container having a limited, specified height. Maintaining video's aspect-ratio (which is very important).

Basically, the object-fit: cover does the job fine here. And in Safari works perfectly, the video upscale/downscale inside his container maintaining aspect-ratio.

In Chrome that happens too, but there's no respect for the height of the container. The element surpasses the height of his container and keep growing to the bottom according to the window's width.

object-fit: fill woks well in both browsers, but the problem here is obvious, the aspect-ratio is not respected, deforming the video/image/etc.

Here's what I have:

HTML

<video preload autoplay loop poster="poster.jpg" id="bgvid">
    <source src="image/video.mp4" type="video/mp4">
</video>

CSS

#bgvid {
  width: 100%;
  min-width: 100%;
  height: 445px;
  max-height: 445px;
  background-color: #f0f0f0;
  object-fit: cover; /* cover works perfectly on Safari */
}

My question is, how can I make this work perfectly respecting the container height (or at least min-height or max-height), responsively in all browsers, keeping the element's aspect-ratio?


like image 470
Peterson Cordeiro Avatar asked Jun 08 '15 01:06

Peterson Cordeiro


People also ask

Why is object-fit contain not working?

For object-fit to work, the image itself needs a width and height . In the OP's CSS, the images do not have width and/or height set, thus object-fit cannot work. ...and the browser will know that this div should completely fill its container's space.

What is object-fit cover?

The object-fit CSS property sets how the content of a replaced element, such as an <img> or <video> , should be resized to fit its container. You can alter the alignment of the replaced element's content object within the element's box using the object-position property.

Does object-fit work with background image?

Adding a Background to an Image With object-fit: contain # We would benefit from that when also using object-fit: contain . In the example below, we have a grid of images. When the aspect ratios of the image and the container are different, the background color will appear.

How do I make an image fit in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels. For example, the original image is 640×960.


2 Answers

I've just hit this myself. It looks like if you wrap your video element in a div and set overflow as hidden, then it'll work around Chrome's bug, i.e. something like:

<div class="wrapper">
  <video preload autoplay loop poster="poster.jpg" id="bgvid">
    <source src="image/video.mp4" type="video/mp4">
  </video>
</div>

with css

#bgvid {
  width: 100%;
  height: 100%;
  background-color: #f0f0f0;
  object-fit: cover; /* cover works perfectly on Safari */
}

.wrapper {
  width: 100%;
  min-width: 100%;
  height: 445px;
  max-height: 445px;
  overflow: hidden;
}

I've also just found a couple of issues file that seem to be covering Chrome's bug on this:

  • https://code.google.com/p/chromium/issues/detail?id=400829
  • https://code.google.com/p/chromium/issues/detail?id=441890
like image 177
Standard8 Avatar answered Sep 21 '22 13:09

Standard8


This is a Chrome rendering bug (per the Standard8's response). An alternative fix is to set a small border-radius ('0.5px' used to be the smallest value that works but I just tested in Chrome 65 and '0.1px' seems to work now) on the video element. This forces the element down a different (and apparently less buggy) rendering path within Chrome.

The advantage of this fix is that it doesn't require a "shrink-wrapped cropping element" around the video. The disadvantage is that cropping to a slightly rounded rectangle is probably slightly less performant than cropping to a rect.

like image 35
podperson Avatar answered Sep 21 '22 13:09

podperson