Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simulate background-size:cover on <video> or <img>

How can I simulate the functionality of background-size:cover on an html element like <video> or <img>?

I'd like it to work like

background-size: cover; background-position: center center; 
like image 372
ivavid Avatar asked May 29 '12 10:05

ivavid


People also ask

How can I set cover photo on IMG tag?

Just set object-fit: cover; on the img . See MDN - regarding object-fit: cover : The replaced content is sized to maintain its aspect ratio while filling the element's entire content box. If the object's aspect ratio does not match the aspect ratio of its box, then the object will be clipped to fit.

How do I cover a video in HTML?

If the video's parent element is set to cover the entire page (such as position: fixed; width: 100%; height: 100vh; ), then the video will, too. Of course, vw , vh , and transform are CSS3, so if you need compatibility with much older browsers, you'll need to use script. This is the best answer.

What is the difference between background-size cover and contain?

cover tells the browser to make sure the image always covers the entire container, even if it has to stretch the image or cut a little bit off one of the edges. contain , on the other hand, says to always show the whole image, even if that leaves a little space to the sides or bottom.

What property of background-size would display the whole image within the element?

The background-size CSS property sets the size of the element's background image. The image can be left to its natural size, stretched, or constrained to fit the available space.


2 Answers

This is something I pulled my hair out over for a while, but I came across a great solution that doesn't use any script, and can achieve a perfect cover simulation on video with 5 lines of CSS (9 if you count selectors and brackets). This has 0 edge-cases in which it doesn't work perfectly, short of CSS3-compatibility.

You can see an example here (archived)

The problem with Timothy's solution, is that it doesn't handle scaling correctly. If the surrounding element is smaller than the video file, it isn't scaled down. Even if you give the video tag a tiny initial size, like 16px by 9px, auto ends up forcing it to a minimum of its native file-size. With the current top-voted solution on this page, it was impossible for me to have the video file scale down resulting in a drastic zoom effect.

If the aspect ratio of your video is known, however, such as 16:9, you can do the following:

.parent-element-to-video {     overflow: hidden; } video {     height: 100%;     width: 177.77777778vh; /* 100 * 16 / 9 */     min-width: 100%;     min-height: 56.25vw; /* 100 * 9 / 16 */ } 

If the video's parent element is set to cover the entire page (such as position: fixed; width: 100%; height: 100vh;), then the video will, too.

If you want the video centered as well, you can use the surefire centering approach:

/* merge with above css */ .parent-element-to-video {     position: relative; /* or absolute or fixed */ } video {     position: absolute;     left: 50%; /* % of surrounding element */     top: 50%;     transform: translate(-50%, -50%); /* % of current element */ } 

Of course, vw, vh, and transform are CSS3, so if you need compatibility with much older browsers, you'll need to use script.

like image 159
M-Pixel Avatar answered Sep 22 '22 02:09

M-Pixel


For some browsers you can use

object-fit: cover; 

http://caniuse.com/object-fit

like image 38
Daniël de Wit Avatar answered Sep 23 '22 02:09

Daniël de Wit