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?
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.
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With