Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 video with fixed height, but scale width to 100%

Tags:

html

css

What I want to do is have an HTML5 video scale 100% width of the page, but maintain a fixed height of 650px.

The following code scales to maintain aspect ratio, which is not what I need:

<header>
    <video width="100%" autoplay="autoplay">
        <source src="video.webm" type="video/webm; codecs=vp8,vorbis">
    </video>
</header>

I also tried a max-height="650px" but this only centres the video and leaves whitespace on either side.

like image 772
dantan04 Avatar asked Oct 20 '14 18:10

dantan04


People also ask

How do I change the video ratio in HTML?

HTML Video Tags All you need to do is set the width to 100% and the height to auto. This will create a video that fills the entire parent and its height will be calculated automatically based on the width so that it maintains its current aspect ratio no matter the screen size.

How do I make my video fit in a div?

To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.


1 Answers

What paypal does is scaling up the video according to the viewport. But they dont go mobile, and this is a problem.

So if you want to scale your video from small to big devices, you can put your video with the basic markup:

<video width="100%" height="auto">...

This is going to scale up your video. The problem is when you go to a small viewport. The video will scale down but can be too small, so you can define a min-height and use CSS transforms to scale up the video aspect:

video{
transform: scale(1.5);
-webkit-transform: scale(1.5);
}

With media queries you can define breakpoints and scale the video for those screens.

Also with some javascript you can also define a point of focus for your video (if some area of the video is more important).

Check this link for more details on that:

http://viget.com/extend/fullscreen-html5-video-with-css-transforms

like image 194
Gustavo Avatar answered Sep 21 '22 18:09

Gustavo