I'm trying to make a responsive video background like this:
HTML
<video id="bgvid" autoplay="true" loop="true" preload="auto" poster="img/code-landing-page/desktop-bg-2.png">
<source src="videos/code-promo-bg.mp4" type="video/mp4">
</video>
CSS
html, body{
margin:0px;
padding: 0px;
height: 100%;
width: 100%;
overflow: hidden;
}
video#bgvid {
position: absolute;
min-width: 100%;
min-height: 100%;
z-index: -100;
width: 120%;
height: auto;
//width: auto;
//height: 100%;
}
But when window height is big enough this solution leaves a white gap at the bottom. Is there a way to avoid it, as in www.y.co for example?
I would try this:
video#bgvid {
z-index: -100;
min-width: 100vw;
min-height: 100vh;
}
The reason behind it is when you use just height: 100vh or width: 100vw browser will fit the video to the screen and the smaller side wins. With min-* the longer side wins and due to overflow: hidden in body styles it'll be cut away. (In my tests the video didn't look distorted)
vw and vh are units where 100vw means whole width of the browser screen and 100vh whole height of the browser screen.
It might be worth looking into this post on css-tricks, there is a solution at the bottom
Try with object-fit: cover on the video tag.
Object Fit CSS
Related
I found this JSFiddle which does pretty much exactly what I'm trying to accomplish - https://jsfiddle.net/q1rjg9zd/
Notice that you can resize the output frame, and the video will always fill the entire frame, without any space above/below the video to maintain the 16:9 aspect ratio.
I'm trying to get this exact same effect, except I'd like it to be done in a wrapper div, instead of filling the whole viewport. E.g. I can have a div that is 1000px in height and 500px in width and the video would behave the same as in this example.
I'm really struggling to work out how to do this, since the JSFiddle example uses vw and vh measurements, and when I try to convert those to percentages and fixed pixel measurements, the effect is lost.
* {
padding: 0;
margin: 0;
}
.video-background {
position: relative;
overflow: hidden;
width: 100vw;
height: 100vh;
}
iframe {
position: absolute;
top: 50%;
left: 50%;
width: 100vw;
height: 100vh;
transform: translate(-50%, -50%);
#media (min-aspect-ratio: 16/9) {
height: 56.25vw
}
#media (max-aspect-ratio: 16/9) {
width: 177.78vh
}
}
<div class="video-background">
<iframe src="https://www.youtube.com/embed/biWk-QLWY7U?controls=0&showinfo=0&rel=0&autoplay=1&loop=1&mute=1" frameborder="0" controls=0 allowfullscreen></iframe>
</div>
I am trying to get a YouTube video to display at 100% width and height. It seems the iframe itself is doing what I want, however, once the video plays it is only 100% width up to 1100px.
I have tried searching the web but cannot find a solution to get the video itself to be the same width as the video poster and iframe width.
Try this:
.container {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%;
}
.video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="container">
<iframe src="https://www.youtube.com/embed/ZLyS2wHiZM8"
frameborder="0" allowfullscreen class="video"></iframe>
</div>
Reference article
I assume your video ratio is 16:9. Maybe you can try this:
.container {
width: 100vw;
height: 56.25vw;
}
.video {
width: 100%;
height: 100%;
}
This is an example link
Your video has already a 100% width. When I make a right-click on the left or right side of the video, the context menu of the youtube video appears. Check this screenshot
I think the problem is about your video. Your video don't have the right width and height to appear correctly.
I have a simple html5 video player. I would like to stretch the video height to user screen height.
I have tried a lots of things, for ecxample this one:
height : auto !important; /* Ignored by Internet Explorer, applied everywhere else. */
height : 100%; /* Internet Explorer treats as min-height. */
min-height : 100%; /* Internet Explorer ignores this. */
But its not working. There is a living demo here:
http://boxy2.com/testvideo.php?url=http://boxy2.com/3n9?download_token=e99fdb10c5a929aa30d0f497d07f260eb16b511503b4520a4bdd48385b048b88&ad=0
That's my problem when I click on fullscreen it follows only the WIDTH property and not the height. If I remove width:100%, than its' size is about 300px height even on fullsrcreen.
The correct solution to this is to use now is to use object-fit:fill via CSS on the <video> tag.
video {
object-fit: fill;
}
CSS
video { height: 100vh; min-height: 100%; }
Most videos are 16:9 ratio, so in order to increase the height to the edges of the screen, you end up with a cropped view. The closest ratio to what you want is the old TV format of 4:3.
vh and vw are layout lifesavers here's more on viewport height and viewport width.
If you're looking for a full-height sol'n that always covers content area:
demo
html
<header role="banner">
<div id="wrapper-video">
<video poster="" autoplay loop>
<source src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/156843/cut.mp4"
type="video/mp4; codecs=avc1.42E01E,mp4a.40.2">
</video>
</div>
</header>
css
section[role="banner"] {
position: relative;
width: 100%;
}
#wrapper-video {
position: fixed;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
z-index: -100;
}
#wrapper-video video {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 50%;
min-height: 50%;
}
edit - working in Opera
If anyone else needs it. You can set for the video this:
video {
height: 100vh;
object-fit: cover;
}
I'm trying to make a full width banner using html5 video I want the width to be 100vw and height roughly 400px this was my attempt;
min-width: 100vw;
width: auto;
z-index: -100;
background: url(polina.jpg) no-repeat;
overflow: none;
It didn't quite work its just full screen with overflow bars on x & y, anyone know how I can achieve this effect.
If I add a hight of 400px it just removes the video width
If you want to do this your HTML would look like
<video controls>
<source src="filename.mp4" type="video/mp4">
</video>
And then your CSS would be
video {
display: block;
width: 100%;
height: 400px;
z-index: -100;
background: red; /* Replace with the path to your background image
overflow: none;
}
Here is a demo
I want to have a site that is 100% of the height of the browser at all times, with the width scaling with an aspect ratio when the height is changed.
I can achieve this using the new vh unit: http://jsbin.com/AmAZaDA/3 (resize browser height)
<body>
<div></div>
</body>
html, body {
height: 100%;
width: 100%;
margin: 0;
}
div {
height: 100%;
width: 130vh;
margin: 0 auto;
background: #f0f;
}
However, I worry about fallback for IE8 and Safari, as it this unit is not supported there.
Are there any other CSS only methods of achieving this effect?
I have a solution that works also with IE8 (using Pure CSS 2.1), but not perfectly.
because I need the browser to recalculate things when he get resized, and apparently it doesn't do that unless he has to (and I cant find a way to make him think he has to), so you will have to refresh the page after resizing.
as far as I know, the only element that can scale reserving his ratio is an <img>, so we will use the <img> to our advantage.
SO, we are going to use an image with the ratio that we want (using the services of placehold.it), lets say we want a 13X10 ratio (like in your example), so we'll use <img src="http://placehold.it/13x10" />.
that image will have a fixed height of 100% the body, and now the width of the image scales with respect to the ratio. so the width of the image is 130% height of the body.
that image is enclosed within a div, and that div has inline-block display, so he takes exactly the size of his content. witch is the size you want.
we remove the image from the display by using visibility: hidden; (not display:none; because we need the image to take the space), and we create another absolute div, that will hold the actual content, that will be right above the image (100% width and 100% height of the common container).
That works perfectly when you first initiate the page, but when you resize the page, the browser doesn't always measure the right width and height again, so you'll need to refresh to make that happened.
Here is the complete HTML:
<div class="Scalable">
<img class="Scaler" src="http://placehold.it/13x10" />
<div class="Content"></div>
</div>
and this simple CSS:
html, body, .Content
{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body
{
text-align: center;
}
.Scalable
{
position: relative;
display: inline-block;
height: 100%;
}
.Scaler
{
width: auto;
height: 100%;
margin-bottom: -5px;
visibility: hidden;
}
.Content
{
position: absolute;
top: 0;
background-color: black;
}
Here's a Fiddle (don't forget to refresh after resizing)
I recommend you to copy this code to your local machine and try it there rather then within the fiddle.
In this similar SO question a CSS technique was found and explained on this blog entry that allows an element to adjust its height depending on its width. Here is a repost of the code:
HTML:
<div id="container">
<div id="dummy"></div>
<div id="element">
some text
</div>
</div>
CSS:
#container {
display: inline-block;
position: relative;
width: 50%;
}
#dummy {
margin-top: 75%; /* 4:3 aspect ratio */
}
#element {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: silver /* show me! */
}
Demo Here
If this is sufficient for you, I'd recommend this technique. However, I'm unaware if the technique can be adapted to handle scenarios where you must have an element adjust its width depending on its height.
You can do it with the help of padding on a parent item, because relative padding (even height-wise) is based on the width of the element.
CSS:
.imageContainer {
position: relative;
width: 25%;
padding-bottom: 25%;
float: left;
height: 0;
}
img {
width: 100%;
height: 100%;
position: absolute;
left: 0;
}