Cover-Video in banner element: always centered and full-width and height - html

I know there are a lot of questions out there regarding this topic, however I couldn't find a proper solution.
I have a #banner element on top of my site that is 710px high.
In this #banner I have my video that should always scale like "background-size:cover". It doesn't matter if the video is cut at the top or bottom, it should just always fill the #banner element.
#banner {
position:relative;
opacity:0;
height:710px;
width:100%;
overflow:hidden;
}
#banner .video {
position: absolute;
bottom: 50%;
right: 50%;
transform: translateX(-50%) translateY(-50%);
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
overflow: hidden;
}
I found this code right here but it doesn't properly work for me. The video does not resize when scaling the browser-window. For me this does not work.
Also I tried to use the covervid plugin that seems to work perfect for full-background sizes but not to fit it inside a banner with fixed height.
If I use this plugin and resize the window it jumps from width to height fitting, always setting the width or either height to auto.
Any idea how to do this via css or js?

Just remove the opacity from the banner and add max-height:100% to make sure that there's no vertical scroll
DEMO
html,
body {
margin: 0;
height: 100%;
}
#banner {
position: relative;
height: 710px;
border: 5px solid tomato;
overflow: hidden;
max-height: 100%;
box-sizing: border-box;
}
#banner .video {
min-width: 100%;
min-height: 100%;
}
<div id="banner">
<video class="video" autoplay>
<source src="http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4" type="video/mp4" />
</video>
</div>

Related

Setting video as div background and bring the text in front

I want to set my text background as video. Upon my trying I am not able to bring the text in front. The text always hidden behind. Another issue is that the video background is not responsive in smaller screen and it covers the whole screen. More so, it seems that the video's frame width and height are removed (e.g. doesn't play in origianl width and height reatio).
Could anyone please help me solving this problem?
HTML:
<div class="container-fluid no-left-padding no-right-padding welcome-text video-content">
<!-- Container -->
<div class="container">
<video autoplay muted loop>
<source src="assets/videos/welcome_video.mp4" type="video/mp4">
</video>
<h2>Hello, I want to display this text, and play the video on background.
</h2>
</div>
</div>
CSS:
.welcome-text {
padding-bottom: 180px;
padding-top: 180px;
}
.video-content{
height: 100%;
width: 100%;
overflow: hidden;
position: relative;
}
video{
min-width: 100%;
min-height: 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
Try putting<h2>Hello I want to play this text and play the video on the background.<h2\> before <video><video\>tags
I would also advice that parameters
Like height and width be
width:auto;height:auto;
Try to put a z-index: 2 in the text so that it comes to the front of the video.
z-index
Regarding the size of the video, you have to define a size of the div.container and the
video{
min-width: 100%;
min-height: 100%;
[...]
}
Be this example at jsfiddle
I advise you to take a look at these two articles:
video height
how to change height of the video
Bring text in front of the video:
.welcome-text {
position: relative;
/* make it position:absolute instead if you want it to be centered as shown below*/
z-index: 1;
}
Center text in the container:
.container {
position: relative;
}
.welcome-text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
Regarding making the video responsive and mantaining the aspect ratio, you have to figure out what's the aspect ratio. Let's say the video is 1280 x 720, then 1.78:1 is the aspect ratio. It means if the width is 100% the height should be 56%.
You can't tell the div to have 56% of the container's width, but there is a trick, you can tell it to have 56% of the width as padding:
.container {
position: relative;
height: 0;
padding-bottom: 56%;
overflow: hidden;
}
video {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
}

Fitting a YouTube iframe (both horizontally and vertically) without black bars

I want to fit a YouTube iframe embed in the middle of a page (70% width) so I'm doing this:
.wrapper {
margin: auto;
width: 70%;
}
.wrapper iframe {
left: 0;
top: 0;
width: 100%;
height: 100%;
}
The problem is, I'm getting black bars around the video to fill the available space. If instead I used padding (as in this answer) the iframe would only fit horizontally and might require scrolling in the vertical direction. Any idea what might help? (older browser support is not an issue)
Edit: I know the aspect ratio of the video I want to play.
You can try to use flex css for this issue. you can use the static height width to the iframe as per your requirement
.wrapper {
display: flex;
align-items: center;
justify-content: center;
}
.wrapper iframe{
width: 100px;
height: 100px;
}
Black bars appear when the height/width ratio of the iframe is too high (over .5, approximately).
Use this CSS to maintain a fixed height/width ratio, even when the wrapper is resized:
.wrapper {
width: 70%;
padding-top: 35%; // 35/70 = 0.5, the ratio can be changed here
margin: auto;
position: relative;
}
.wrapper iframe {
width: 100%;
position: absolute;
height: 100%;
transform: translate(0, -100%);
}

Bootstrap div height won't resize properly with video background

I have a video playing in the background of my top div. It looks fine when the screen is full size, but when I resize the browser window to see how it will look on smaller screens, the height of the div remains the same, leaving a big empty space between the background video and the next div.
Here is a preview of the site if you'd like to see for yourself, along with the specific code. https://codepen.io/CarlyWysocki/pen/YYaBOd
HTML:
<div class="jumbotron" id="top">
<video autoplay loop>
<source src="https://videos2.sendvid.com/03/25/up5p1yhu.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<div class="container text-center">
<h1>Mac Demarco</h1>
<h4>Canadian singer-songwriter, multi-instrumentalist and producer.</h4>
<i class="fa fa-chevron-circle-down"></i>
</div>
</div>
CSS:
video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: auto;
}
.jumbotron {
padding-top: 30px;
padding-bottom: 30px;
margin-bottom: 30px;
color: inherit;
background-color: #eee;
height: 100%;
overflow: hidden;
position: relative;
}
.container {
position: relative;
}
If it just stays in the center, you can add css media queries resizing appropriately. One could also just use a picture for small screens via media queries.
Using the referenced stack overflow answer:
.video-container {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.video-container video {
/* Make video to at least 100% wide and tall */
min-width: 100%;
min-height: 100%;
/* Setting width & height to auto prevents the browser from stretching or squishing the video */
width: auto;
height: auto;
/* Center the video */
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}

Video doesn't scale correctly in width

I have a slight issue. I have a video which I would like to adjust to the browser, it shouldn't stretch, but neither should there be any with space visible
HTML:
<video src="Wereldbol.mp4" onclick="this.play();" id='wereldbol' preload="auto" ></video>
CSS:
#wereldbol {
position: absolute;
bottom: 0px;
right: 0px;
width: 100%;
height: 100%;
z-index: -1;
overflow: hidden;
background-size:cover;
}
background-size: cover; is causing the issue that the video would be off-center, is there any alternative way to cover the browser's full width and height? At the moment width: 100% and height: 100% don't quite fix the issue because it would leave the image to have a white bar on the left and right, eventhough the video scales correctly. Is there any way to fix this issue?
If I understand you correctly, what you are trying to achieve is keep the video’s aspect ratio, but make it adapt to its surroundings without stretching the video out of proportion. Here’s how I do it:
Wrap the video element in a div, like this:
<div class="video-wrapper">
<video src="Wereldbol.mp4" onclick="this.play();" id='wereldbol' preload="auto" ></video>
</div>
Then use the following CSS:
.video-wrapper {
position: relative;
width: 100%;
max-width: 100%;
height: 0;
padding: 56.25% 0 0 0; /* 100%/16*9 = 56.25% = Aspect ratio 16:9 */
overflow: hidden;
border: 0;
}
.video-wrapper video {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
EDIT: Here’s a plunker: Adjust the viewport-width to see how it works.
You need to either adjust the CSS to fit the exact aspect-ratio using the "aspect ratio trick" or use something like FitVids.js -- see http://fitvidsjs.com/

How can I make youtube video responsive along with background image?

I have responsive background and I want to have a YouTube video over that background(not in full width).
Here I have tried doing it.
http://jsfiddle.net/audetwebdesign/EGgaN/#run
HTML:
<div class="bg-image">
<img src="http://unplugged.ee/wp-content/uploads/2013/03/frank2.jpg">
<iframe width="560" height="315" src="//www.youtube.com/embed/R8wHnwfHscw" frameborder="0" allowfullscreen></iframe>
</div>
CSS:
.bg-image {
position: relative;
}
.bg-image img {
display: block;
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
.bg-image iframe {
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
Here's a jsfiddle forked from your fiddle that has the image as the background, as well as a responsive youtube video centered. Making the image have position:absolute takes it out of the normal flow and allows the embedded video to stay on top.
The trick for the responsive video code is to wrap the embedded video in a container with a max width, and then also adding in padding to keep the proper aspect ratio for the video. You then ensure that the iframe, object, and embded elements all fit at 100% of that container's width while also not getting any taller than the native size:
.video-container {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
width: 100%;
max-width: 560px;
margin: 0 auto;
}
.video-container iframe,
.video-container object,
.video-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
max-height: 320px;
}
http://jsfiddle.net/QRkL9/
More about the above code - http://avexdesigns.com/responsive-youtube-embed/
braican is correct, but the 56.25% on the video container will leave lots of padding after your video. Just wrap everything inside another div with a max-height of 320px and overflow:hidden to hide the extra padding;