Giacomo Lawrance Giacomo Lawrance - 10 months ago 61
HTML Question

Making an iframe resize according to the window size

I'd like to make the size of the video relative to the screen size. However, if I set the height as fixed, on some screen sizes it does not work. Any way I can get the video to fit the screen, but not be out of proportion?

The full code is here:

Thank you!

Answer Source

Wrap the video in another element which has an intrinsic aspect ratio, then absolute position the video within that. That gives us fluid width with a reasonable height we can count on.

<div class="videoWrapper">
<iframe src='' width='100%;' height='500px;' frameborder='0' scrolling='no'></iframe>

And then apply the following style properties..

.videoWrapper {
    position: relative;
    padding-bottom: 56.25%; /* 16:9 */
    padding-top: 25px;
    height: 0;
.videoWrapper iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

May be that helps..