Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate a video and fit it to screen using CSS

Tags:

html

css

video

mp4

I have the following style:

video {
    position: absolute;
    transform: rotate(90deg);
    width: 100%;
    height: 100%;
    z-index: 4;
    visibility: visible;
}

This is the video element:

<video id="myVideo" src="/Space4.mp4" autoplay loop></video>

This seems to rotate and center the video, but it is almost 1/4 of the screen size. How can make it fit to screen?

like image 418
Pavan K Avatar asked Jan 29 '16 14:01

Pavan K


People also ask

How do you flip a video in CSS?

You can use CSS3 scaleX or scaleY set to -1 to respectively flip the video horizontally or vertically.

How do I rotate a Div 90 degrees?

We can add the following to a particular tag in CSS: -webkit-transform: rotate(90deg); -moz-transform: rotate(90deg); -o-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg);

How to rotate an image in CSS?

Good news, it is possible to rotate images in modern CSS. To can rotate an image in CSS, simply use the transform rotate property. For example, img.rright { transform: rotate (90deg) } That covers the basics, but we can actually do more and animate a spinning image using rotate.

How to rotate an HTML element using CSS transform property?

The purpose of this article is to rotate an HTML element by using CSS transform property. This property is used to move, rotate, scale and to perform various kinds of transformation of elements. The rotate () function can be used to rotate any HTML element or used for transformations. It takes one parameter which defines the rotation angle.

What are the units of rotation in CSS?

The unit can be defined in degrees (deg), gradient (grad), radians (rad) and turns. Example: The following example demonstrates rotation of an image by 45 degree. How to rotate an element using CSS?

How do I style a video in HTML5?

The HTML5 video tag allows you to style the video with CSS and CSS3 transforms allow both for scaling and for rotation. So let’s put those together. Embedding a video is as simple as this (see this in action on the demo page ):


1 Answers

This is a case where the new CSS3 units come in handy. If you just use normal percentages to specify the width and height of the <video> element, they will default to associating these dimensions with their viewport counterparts - but only prior to the rotation. So after rotation, these values will no longer correspond correctly to the viewport dimensions.

Since you actually want the opposite in this case, you can use height: 100vw and width: 100vh to explicitly specify that you want height measured in terms of viewport width, and width in terms of viewport height.

With the correct sizing, you'll also need to change the point around which the video is rotated. Otherwise, it becomes difficult to align the edges of the video with the edges of the viewport, as shown in this expertly crafted visual example:

enter image description here

Following this adjustment, the last step is just to move the video upwards by a certain amount, in order to make it flush against the top of the viewport. How much is that amount? Well, the height of the video - which we specified as 100vw. (I used a negative margin-top for this.)

Implementing these changes (and setting object-fit: cover so no whitespace is visible), we end up with:

html,
body {
  margin: 0; /* Because annoying default browser margins */
}
video {
  position: absolute;
  transform: rotate(90deg);

  transform-origin: bottom left;
  width: 100vh;
  height: 100vw;
  margin-top: -100vw;
  object-fit: cover;

  z-index: 4;
  visibility: visible;
}
<video id="myVideo" src="http://html5demos.com/assets/dizzy.mp4" autoplay loop></video>

Hope this helps! Let me know if you have any questions.

like image 91
Serlite Avatar answered Nov 15 '22 23:11

Serlite