I would like to create a full-screen video background on my website, ideally using only HTML and CSS. I also need the video background to be stuck to the top of the page, so the users first view is entirely covered by the video and then they can scroll down and move past the video.
I need the video to be responsive and preserve the aspect ratio. However, to avoid black bars, I would like the video to fill 100% of which ever dimension (width or height) is the smallest, and then overflow the video for the other dimension.
This is my solution so far...
<div class="container-fluid adjustedHeight">
<div class="video-container">
<div class="row">
<div class="col-md-12"></div>
</div>
<div class="row Page1">
<div class="col-md-6 button1">
<button type="button" class="btn btn-primary btn-lg outline">Pre-Order</button>
</div>
<div class="col-md-6 button2">
<button type="button" class="btn btn-primary btn-lg outline">About Us</button>
</div>
</div>
<video autoplay loop class="fillWidth">
<source src="Images/Comp 2.mp4" type="video/mp4" />
Your browser does not support the video tag. I suggest you upgrade your browser.</video>
<div class="poster hidden"> <img src="http://www.videojs.com/img/poster.jpg" alt=""> </div>
</div>
</div>
CSS:
.video-container {
position: relative;
bottom: 0%;
left: -15px;
height: 100% !important;
width: 100vw;
overflow: hidden;
background: #000;
display: block !important;
}
.video-container video {
position: absolute;
z-index: 0;
bottom: 0;
}
.video-container video.fillWidth {
min-width: 100%;
min-height: 100%;
}
It works for the width, but I get black bars if the height is too big for the video, i.e. the width won't overspill. How can I define this to ensure that both the width and height are at least 100% filled? I thought the min-height
and min-width
attributes would have done this, but this scales both height and width beyond 100% and when I resize, the video doesn't scale, it just cuts off more of the video's width (and doesn't keep the video central). Whereas, if I just use the width and height definitions in the .video-container video.fillWidth
CSS attribute, I do get the video centrally, but I get black bars.
Here is a JSFiddle It currently shows that black bars are appearing above and below (also on the left and right, although this is hard to see) when the aspect ratio is different to the viewport ratio.
NB: I am using bootstrap 3 and my adjustedheight
attribute allows space for the navbar.
EDIT:
To answer the close requests, the linked question has answered which require JS, which I don't want to use.
Thanks for your help, I would really love to get a resolution to this as it doesn't seem to make sense based on what my code should be doing - not sure where I am going wrong...
Does anyone have any thoughts? Since my JSFiddle everything seems to have gone quiet...