Html5 video background, keep center of video in center
here's how I typically do background video, and how I did it for the stre.am landing page:
.video_contain {
position: absolute;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
}
video {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
margin: auto;
min-height: 50%;
min-width: 50%;
}
HTML5 Video centering source
You probaly mean the object-fit
property in CSS. This will scale the video to fill the video container element, keeping its ratio intact.
You can change the height of the video in the snippet below. You will notice the video will scale itself to the exact height of its container.
With object-fit: cover;
body, html { padding: 0; margin: 0; height: 100%; width: 100%;}
video { height: 100%; width: 100%; object-fit: cover;}
<video controls> <source src=http://techslides.com/demos/sample-videos/small.webm type=video/webm> <source src=http://techslides.com/demos/sample-videos/small.ogv type=video/ogg> <source src=http://techslides.com/demos/sample-videos/small.mp4 type=video/mp4> <source src=http://techslides.com/demos/sample-videos/small.3gp type=video/3gp></video>
How can I center an image over a video using HTML and CSS?
#wrapper {
position: relative;
display: inline-block;
}
#reel {
width: 100vw;
}
.overlay {
position: absolute;
width: 40%;
left: 30%;
top: 50%;
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
<div id="wrapper">
<video playsinline autoplay muted loop id="reel">
<source src="http://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
</video>
<img class="overlay" src="https://www.w3schools.com/images/driveicon_32.png">
</div>
center fullscreen background video
Here's a JQuery function I wrote a long while back to make a video a fullscreen background. Essentially if the aspect ratio of the window is taller than the aspect ratio of the video then make the height 100% and width auto and vice-versa for wider aspect ratio windows.
// Resize the video elements so that we don't get any borders due to aspect ratio
function resizeVideo() {
if ($(window).height() > $(window).width() * 0.5425) { // Which dimension is bigger dependant on aspect ratio (16:9)
$("video").removeAttr("height").removeAttr("width").width("auto").height("100%");
}
else {
$("video").removeAttr("height").removeAttr("width").width("100%").height("auto");
}
};
// Add the resize function to the window resize event but put it on a short timer as to not call it too often
var resizeTimer;
$(window).resize(function () {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(resizeVideo, 150);
});
Scaling HTML5 video and maintain center?
You can use jQuery to center the video by changing the top
and left
attributes.
$(window).resize(function () {
var $vid = $('#fullscreen_video');
$vid.css({
left: ($(window).width() - $vid.width()) / 2,
top: ($(window).height() - $vid.height()) / 2
});
});
Related Topics
Ie8 Ignores Td Width, Works on Ie7
Ie Uses Courier Font When "Font-Family: Monospace" Is Used
How to Select Multiple Ids in CSS
Wrapper Div Height Is 0 with Floated Elements Inside
How to Vertically Align Label and Input in Bootstrap 3
Two Divs; Left Should Be Fixed Width, Right Should Fill Rest of Space
Accessing The Shadow Dom Elements in Cypress
Custom Fields for Shopify User Registration Form
How to Create Multiple Columns in a Div
Will Whitespace or Wrap Be Ignored Between Two Block Elements in HTML
Supporting Multiple Resolution and Density of Images in Phonegap
Why Is There Excessive Whitespace with Facebook Mobile Comments Plugin
Why <Big> Is Not in HTML 5 Tag List While <Small> Is
Integrate Less Version of Bootstrap-Material-Design to Rails Project
How to Put a Vertical Line Down The Center of a Div
What Does Multiple Class Definition Mean and How Is It Supposed to Work