Make video fit 100% with any screen resolution
Solution 1
Found a good solution here: http://codepen.io/shshaw/pen/OVGWLG
So your CSS would be:
.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%);
}
HTML:
<div class="video-container">
<video>
<source src="~/Videos/myvideo.mp4" type="video/mp4" />
</video>
</div>
Solution 2
You can now use the object-fit property. This property has been designed especially to manage responsive size for <img>
and <video>
elements. It is now supported by all modern browsers.
.videosize {
position: absolute;
z-index: -1;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
Solution 3
.video-container {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
overflow: hidden;
object-fit: fill;
}
.video-container video {
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
This worked for me.
Solution 4
This worked amazingly: https://css-tricks.com/fluid-width-video/
video {
/* override other styles to make responsive */
width: 100% !important;
height: auto !important;
}
Solution 5
I Set the height of the video Tag and it solved the problem:
<video height="600" autoplay="true" loop="true" muted="true" plays-inline="" style="position: absolute; right: 0; top: 0; min-width:100%; z-index: -100; object-fit: cover;">
<source src="assets/vid/grapes.mp4" type="video/mp4"> </video>
Qwerty
Updated on July 26, 2022Comments
-
Qwerty almost 2 years
I have a video with the following properties, Frame width: 1920 and Frame Height: 1080. I need its width and height to be 100% thus filling up the whole screen. And it needs to be responsive too. So far, I have this code :
<video class="hidden-xs hidden-sm hidden-md hidden-custom videosize embed-responsive-item" autoplay="autoplay" loop="loop"> <source src="~/Videos/myvideo.mp4" type="video/mp4" /> </video>
css:
.videosize { position:absolute; z-index:-1; top:0; left:0; width:100%; height:100vh; }
With the code above it fits perfectly with a 1680 x 1050 screen resolution, however with other resolution, it takes up 100% of the height then the width adjusts leaving white spaces on both sides.
Any idea ? Thanks.
-
Martijn almost 7 yearsOh please dont. This is outdated, even for a 2016 answer
-
Nicholas about 6 yearscan you post a full example of this?
-
David about 6 yearsFor now, @raumus solution is definitely better than my
object-fit
solution because it is not yet supported by Microsoft Edge (v16.16299)... :-( -
Nicholas about 6 yearsAny idea if we could achieve the same thing for an embedded Youtube iframe like this?
-
Pathros almost 6 years` /* Make video to at least 100% wide and tall */ min-width: 100%; min-height: 100%; ` Did the trick for me
-
thinklinux almost 6 yearsOfc Microsoft is behind even with their new browser. Otherwise object-fit is really nice!!!
-
Stefan Midjich over 5 yearsThis won't work with vertical videos, for example 1080x1920 resolution. The video looks slightly zoomed in so parts of the top and bottom are cut off. I'm still looking for a solution myself, will return here if I find one.
-
sheriffderek over 5 years@StefanMidjich - that's just the way it goes.
-
Raja over 5 yearsIn my case I wanted the video to fit neatly in the available space, i.e. scale up only until it touched either the vertical or horizontal edges. I found
object-fit: contain
did that perfectly. -
Stefan Reich over 3 yearsWhat is that height for? Why 600? Also, plays-inline should be playsinline