html, body {
height: 100%;
margin: 0;
padding: 0;
}
.sized {
height: 100%;
position: relative;
background: #eee;
overflow:hidden;
padding:0;
}
.sized iframe {
position:absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
@media (min-width: 320px) {
height: 200%;
top: -50%;
}
@media (min-width: 640px) {
height: 180%;
top: -40%;
}
<div class="sized">
<iframe src="https://player.vimeo.com/video/135335257?autoplay=false" frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
</div>
<h3>Original video</h3>
<iframe src="https://player.vimeo.com/video/135335257?autoplay=false" frameborder="0" webkitallowfullscreen="" mozallowfullscreen="" allowfullscreen=""></iframe>
As I get a cookies same origin error in the snippets result, here is a mirror:
https://jsfiddle.net/07Lffw5x/2/embedded/result/
[edit] Maybe this is a better demo, if you compare to this one, there is not much difference... why? [/edit]
I'm trying to reproduce a background-size cover for an iframe.
The thing is that it seems to rescale the video, for bigger sizes only,
Question,
Can the rescales take effect on every breakpoint? or the vimeo player might rescale by it's own anyway?
Ok. The merit is NOT mine as I got the jquery here
I remembered that question as I used it on one of my old projects and I wanted to check if it would work the same with an iframe. It does.
basically with this css:
and this jquery:
You get this: JSFIDDLE
(I know you were looking for a pure css solution, which I don't think it's possible but I can be wrong, but I have posted this answer because it could help other people with same issue)
Similar to Alvaro Menendez's answer, credit needs to go to this answer stackoverflow.com/a/29997746/3400962 by Qwertman. I got as far as using the "padding percentage" trick, but this answer's clever use of viewport units is crucial to this working.
The key to implementing this behaviour is to ensure two things:
iframe
always maintains the same aspect ratio as its video content 16 : 9. This will ensure that no black "padding" is present around the outside of the videoiframe
always fills theheight
orwidth
depending on the size of the viewportOne way to maintain the aspect ratio of an element is to use the "padding percentage" trick which takes advantage of the fact that
top
andbottom
padding
uses thewidth
of the element as the basis for their value. Using the formula B / (A / 100) = C% we can calculate the required percentage for the padding. Given the video has a 16 : 9 ratio this translates to 9 / (16 / 100) = 56.25.The only problem is that in your case the calculation is required for both the horizontal and vertical axis (as we don't know what dimensions the viewport will be) and this trick will not work with
left
andright
padding
to get the aspect ratio in relation to theheight
.https://jsfiddle.net/w45nwprn/ (Snippet doesn't show video, please see fiddle)
Luckily, in your case you want the video to fit the entire screen so viewport units can be used to calculate the aspect ratio instead of percentages. This allows use to calculate the
width
in relation to theheight
and vica versa:left: 50%;
,top: 50%;
andtransform: translate(-50%, -50%);
are required to center theiframe
in.container
min-height: 100%;
andmin-width: 100%;
are required to ensure that theheight
andwidth
are never smaller than that of.container
height: 56.25vw;
will set theheight
in relation to thewidth
of the viewport. This is calculated by doing 9 / (16 / 100) = 56.25width: 177.77777778vh;
will set thewidth
in relation to theheight
of the viewport. This is calculated by doing 16 / (9 / 100) = 177.77777778Because the
height
andwidth
can never be below100%
but the must remain in the correct aspect ratio the video will always cover the whole viewport.https://jsfiddle.net/qk00ehdr/ (Snippet doesn't show video, please see fiddle)
Viewport units are widely supported, so as long as you are not targeting old browsers this method should work.