Rotate a video and fit it to screen using CSS - html

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?

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:
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.

Related

Video.js HTML/CSS Fluid Contain within Container

I have a standard Video.js player with the fluid property set to true like the following.
<link href="https://vjs.zencdn.net/6.9.0/video-js.css" rel="stylesheet">
<div class="video">
<video id="my-video" class="video-js" data-setup='{"fluid": true}' controls preload="auto" width="640" height="264" poster="MY_VIDEO_POSTER.jpg" data-setup="{}">
<source src="http://vjs.zencdn.net/v/oceans.mp4" type='video/mp4'>
</video>
</div>
<script src="https://vjs.zencdn.net/6.9.0/video.js"></script>
I also have the following CSS.
.video {
width: 500px;
height: 100px;
}
Now technically in the actual site those width and height properties are more dynamic. Depending on page size and all that. So it's not like I'll know what the width and height will be.
Currently what happens in both the example I made to reproduce it, and my site is that the fluid only maps to the width, and doesn't take into account the height of the container.
What I'm looking to do is make the video fluid (no black bars around video) and contain it within the width and height of the parent div. If the video needs to be smaller than the container div in terms of width (due to fluidness) it should center within the div.
I have posted a CodePen below as an example of the code above.
https://codepen.io/anon/pen/YvxbJB
Any ideas how I can achieve this?
One thing you can try is to set your container div to a relative position, center that div, and then set padding to get the aspect ratio you want (if any). Then absolutely position your video tag with a height and width of 100%.
.video {
position: relative;
/* padding-bottom: 56.25%; uncomment to get an aspect ratio of 16:9 */
padding-top: 25px;
height: 0;
margin: 0 auto;
}
#my-video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
Hope that helps!

margin-top change based on content height

I've got a video that I'm using as a background above the fold on a page, spanning 100% width. I want the video to be situated at almost the center point of the page. I figured the best option was to have the height set to something along the lines of using vh. However, I noticed that when I get to larger screens, since the video itself re-sizes to the larger width, it makes the video height larger as well, resulting in the whole bottom being cut off.
This is what I have:
.container {
position: relative;
margin-top: 20vh;
}
.video {
opacity: .2;
width: 100vw;
vertical-align: middle;
height: auto;
}
Is there a way to figure out how what the height of the video is, which then I could use to figure out how much padding I can add at the top as blank space? Or is there an even easier method that I'm over-looking?
Thanks!
Edit to add HTML
Here's the HTML for comparison:
<div class="container">
<video autoplay muted loop class="video">
<source src="./media/MockUpVid.mp4" type="video/mp4"/>
</video>
</div>
.container {
position: relative;
margin-top: calc((100vh - 56vw)/2);
}
.video {
opacity: .2;
width: auto;
vertical-align: middle;
min-height: 60vh;
}
Using the aspect ratio we were able to achieve the desired layout.

Fill a div with an image respecting aspect ratio

Is it possible to fill a div with an image such that at least one image dimension is 100% and the other dimension is either wider or equal size as the div, whilst respecting the image's aspect ratio.
An example could use the classes wide and tall like this:
<div class="tall">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/ae/Klaproos.jpg/266px-Klaproos.jpg"/>
</div>
<div class="wide">
<img src="https://groenevrijdag.files.wordpress.com/2013/11/klaproos2.jpg"/>
</div>
div {
width: 400px; height: 400px;
display: block;
overflow: hidden;
border-radius: 50%;
display: inline-block;
}
div.tall img { width: 100%; margin-top: -50%; }
div.wide img { height: 100%; margin-left: -50%; }
https://jsfiddle.net/7tuod6vu/
I'm looking for a pure HTML+CSS solution which works for responsive rectangular (not necessarily square) divs. For this particular reason, Javascript would be a pain as one would need to determine whether the width or height should be 100% on every resize. Server side wouldn't even be an option.
Does a pure HTML+CSS solution exist for this?
EDIT Should have been clear about this from the beginning, sorry about that :( I'm not looking for the background-image solution, since it does not allow base64-inhtml representation of images. Moreover, I think background-image's are semantically different from <img>s.
Consider using the CSS object-fit property.
5.5. Sizing Objects: the object-fit
property
The object-fit property specifies how the contents of a replaced
element should be fitted to the box established by its used height and
width.
Here are two of the values:
cover
The replaced content is sized to maintain its aspect ratio while
filling the element's entire content box.
contain
The replaced content is sized to maintain its aspect ratio while
fitting within the element's content box.
So, with cover the image retains its aspect ratio and covers all available space. Of course, this means that much of an image may be cropped off-screen.
With contain the aspect ratio is also maintained, but the image scales to fit within the box. This means that an image may have whitespace on the left and right, or top and bottom.
Browser Compatibility
As of this writing, object-fit is not supported by Internet Explorer. For a workaround see:
Neat trick for CSS object-fit fallback on Edge (and other browsers)
fitie - An object-fit polyfill for Internet Explorer
object-fit-images - Adds support for object-fit on IE9, IE10, IE11, Edge and other old browsers
Polyfill (mostly IE) for CSS object-fit property to fill-in/fit-in images into containers.
More information
MDN object-fit property
CSS-Tricks object-fit property
object-fit browser support # caniuse.com
Here is the solution without using background images and with HTML and CSS only: http://codepen.io/anon/pen/JGGObQ
(change overflow to visible in the .container1 rule to see the full pictures. The numbers in them are their original size in pixels.)
It uses position: absolute on the images, and depending on the format (two classes, as suggested by yourself) a top or left of 50% that moves the position reference into the (horizontal or vertical) center, and a transform : translate setting that moves the position reference point of the image back from that center by 50% of their own size, which results in centering:
.container1 {
position: relative;
float: left;
margin-right: 50px;
width: 400px;
height: 400px;
border-radius: 50%;
overflow: hidden;
border: 1px solid red;
}
img.landscape {
position: absolute;
width: auto;
height: 100%;
transform: translate(-50%, 0);
left: 50%;
}
img.portrait {
position: absolute;
width: 100%;
height: auto;
transform: translate(0, -50%);
top: 50%;
}
<div class="container1">
<img src="http://placehold.it/750x500/09d/fff" class="landscape">
</div>
<div class="container1">
<img src="http://placehold.it/600x900/0d9/fff" class="portrait">
</div>
This is not the exact solution, but it could be an implementation that you could try to make your code work. Here is an example:
As you can't predict the aspect ratio of the image here is what I would do:
HTML:
Set the div tag to 'fill':
<div class="fill"></div>
CSS:
This will place your image as the background, and stretch it to fit the div size without distortion.
.fill {
overflow: hidden;
background-size: cover;
background-position: center;
background-image:"path/to/image.jpg";
}
You could set the images as the div's backgrounds instead and use backkground-size:cover
https://jsfiddle.net/3x5x0v24/

Want to hide scrollbar when a absolute div already has a 100% height

I am having a client's Html Site Project, Where I used a video in the background of site's homepage, I used a absolute div outside of video with 100% height and width, My Client don't want a scrollbar on y-axis & I also cant use overflow:hidden; property of CSS, may be Client will adds some content in future, I am still confused if i have given 100% height and width to parent element of video then from where the scrollbar is coming when I use bottom:0 poperty with that div then scrollbar won't show but the size of video would be changed, why its happening please help me. Thanks in advance & and forgive me if I could not clear the exact problem which I am getting.
Site URL: http://trekoholic.com/site/
I used body { overflow-y: hidden; } as a temporary basis
CSS and HTML:
div#video-player {
left: 0;
position: absolute;
right: 0;
top: 0;
width: 100%;
}
<div id="video-player">
<video width="100%" height="100%" loop="" autoplay="">
<source type="video/webm" src="Video/eat.webm"></source>
</video>
</div>
you have to change
div#video-player {
position: absolute;
}
by
div#video-player {
position: fixed;
}
it works but has a counter, if the video has the largest height to the height of the bottom of the screen will not see, but if I understood correctly, this is desired, even in the future will allow you to add more content and will be seen without problems
if you want the full video display just add height: 100% to div#video-player
div#video-player {
position: fixed;
height: 100%;
}
the counter, if the video has different proportions than the screen may not fill the entire width
so I really hope this helps

Background video that acts like CSS' background size cover of an element, not entire page

I'm trying to code up a new site where there's a big splash background as a "hero" image. Like this site has, but as a video where the image is of the lady unloading the car: https://www.simple.com
Resize and you can see how the image always fits within it's parent.
I put some of the code I've been working on in JSBin here (not making much progress): http://jsbin.com/ruqisa/1/edit
I can't seem to get that same behavior with CSS. I want the video to spill outside the parent either width or height wise depending on the dimensions of the screen.
Has anyone done this with pure CSS? JS I could calculate it manually on resize, but I'd really love to do this with just CSS.
EDIT: I'm not looking for the entire page to be a video, just the header background.
Let's adapt the technique outlined on the blog demosthenes.info.
Option 1 — Scroll with the page
The parent div:
is given a fixed height and overflow: hidden
The video:
is stretched with min-width: 100% and min-height: 100%
is placed behind content with z-index: -1 (can be any negative number)
To center the video:
top and left are set to 50% which places the top-left corner in the middle of the parent div
the video is offset by half the height and width by dragging it halfway up and to the left with transform: translateX(-50%) translateY(-50%);
Simplified example
Here is a full demo with all your markup.
body {
height: 200vh;
margin: 0;
}
div {
height: 500px;
overflow: hidden;
position: relative;
}
video {
position: absolute;
top: 50%;
left: 50%;
min-height: 100%;
min-width: 100%;
z-index: -1;
transform: translateY(-50%) translateX(-50%);
}
<div>
<video autoplay muted loop>
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
</video>
</div>
2 — Fixed full page background
The video:
doesn't need a wrapper, it can be placed anywhere on the page
is made position: fixed and will position itself in relation to the browser window (viewport)
is stretched with min-width: 100% and min-height: 100%
is placed behind content with z-index: -1 (can be any negative number)
To center the video:
top and left are set to 50% which places the top-left corner in the middle of the screen
the video is offset by half the height and width by dragging it halfway up and to the left with transform: translateX(-50%) translateY(-50%);
Simplified example
Here is a full demo with all your markup.
body {
margin: 0 auto;
padding: 50px;
color: #FFF;
width: 600px;
}
video.background {
position: fixed;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
z-index: -1;
transform: translateX(-50%) translateY(-50%);
}
<h1>I am content</h1>
<p>I am content too!</p>
<video class="background" autoplay muted loop>
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
</video>
.big-splash video {
position: fixed;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
z-index: -100;
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
background: url(some.jpg) no-repeat;
background-size: cover;
}
DEMO: http://jsbin.com/lunarugibe/1/
Source: Create Fullscreen HTML5 Page Background Video
The video is first positioned absolutely then centered using top, left and transform properties along the X and Y axis. That -webkit- vendor prefix is for safari (Can I use... Support tables for HTML5, CSS3, etc).
The min-height and min-width properties force smaller resolution video to occupy the whole page.
The background property gives a preview image until the video is ready to be played. background: cover is for this preview image, to make it occupy the full area.
and the z-index value makes sure that the video is behind all other elements.
Yes, you can achieve this affect with pure CSS pretty easily:
.container {
height: 600px;
width: 100%;
overflow: hidden;
}
.video {
min-height: 100%;
min-width: 100%;
}
If you want to keep it centered or do a full screen video, you will probably need to use some Javascript for calculating window heights.