Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill video element with poster image even if the poster image is a different aspect ratio than its video element? [duplicate]

Tags:

As it stands right now, this is only tested in WebKit.

When a poster image is set on a <video> element using the poster attribute, this image displays before the user plays the video (default behavior). However, if the poster image is of a different aspect ratio than that of the <video> element that it is set on, whitespace (or possibly blackspace) is seen on either side of the image (left and right or top and bottom) and the image is centered (also, default behavior).

I would like to know how to scale this image up (preferably in CSS) so that it fills the video element, similar to using the CSS3 background-size: cover; value.

Still confused? Perhaps this JSFiddle will help explain: http://jsfiddle.net/jonnymilano/2w2CE/

I would also be interested in answers that forced the image into the video container, warping its height and/or width to fit the dimensions of the video container. However, this answer would only partially solve my issue.

like image 248
jonathanbell Avatar asked Mar 19 '13 19:03

jonathanbell


People also ask

How do I put a video into a poster?

The poster attribute specifies an image to be shown while the video is downloading, or until the user hits the play button. If this is not included, the first frame of the video will be used instead.

What does object-fit cover do?

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.

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.


1 Answers

This question got me thinking and your jsfiddle was useful in solving this (albeit not for IE as it doesn't implement the poster image correctly).

Basically combine what you posted, set the required poster image as the background image of the video element and specify a 2x2 transparent image as the actual poster image.

e.g.

<video controls poster="transparent.png">     <source src="video.mp4" type="video/mp4">     <source src="video.webm" type="video/webm">  </video> 

and

video {     width:305px;     height:160px;      background:transparent url('poster.jpg') no-repeat 0 0;     -webkit-background-size:cover;     -moz-background-size:cover;     -o-background-size:cover;     background-size:cover;  } 

I wrote a bit more about it at HTML5 Video and Background Images.

like image 117
Ian Devlin Avatar answered Nov 02 '22 23:11

Ian Devlin