Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive iframe with max width and height

I have a fixed height container containing both images and iframes. To make the images responsive and preventing both vertical and horizontal overflow I can just set the following CSS:

img {
    max-width: 100%;
    max-height: 100%;
}

This ensures that a portrait image would not overflow vertically and a landscape image would not overflow horizontally.

For iframes, I'm using the "padding-ratio" technique, setting the padding of the wrapper element to the aspect ratio of the iframe (e.g. 56.25% for a 16:9 video):

.iframe-wrapper {
    position: relative;
    height: 0;
    padding-top: 56.25%;
    overflow: hidden;
}

.iframe-wrapper iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

While this means that the iframe scales with the width of the viewport it does not work the same if I change the height of the viewport. Essentially I'd like the iframe to mimic how the images work.

like image 380
Ben Foster Avatar asked Feb 08 '14 15:02

Ben Foster


People also ask

Can iframes be made responsive?

The iframe itself ('the box') can be responsive. But everything inside the iframe is a separate page, and therefore not in the domain of your CSS nor JS.

How can I set my iframe height width and 100%?

To size the <iframe> , we ignore the width="560" height="315" element properties, and instead use the CSS properties width:100%;height:100%; . That's it: a full-width <iframe> with fixed aspect ratio. Enjoy.

Does iframe need width and height?

The standard size of an iframe for desktop is a width of "560" and height of "315." Warning: Editing width but not Height or vice versa will result in a very narrow vertical or horizontal view of your video, so be sure to update both accordingly!


2 Answers

For my uses (embedding videos from Vimeo on a responsive site), this works great in the browsers I've tested in:

@media screen and (max-width: 750px) {
    iframe {
        max-width: 100% !important;
        width: auto !important;
        height: auto !important;
    }
}

It doesn't require image placeholders, so it's a lot simpler.

like image 88
Caitlin M Avatar answered Sep 26 '22 05:09

Caitlin M


This code did the trick for me:

<div class="video-container"><iframe.......></iframe></div>

.video-container {
    position:relative;
    padding-bottom:56.25%;
    padding-top:30px;
    height:0;
    overflow:hidden;
}

.video-container iframe, .video-container object, .video-container embed {
    position:absolute;
    top:0;
    left:0;
    width:100%;
    height:100%;
}

Source: https://www.h3xed.com/web-development/how-to-make-a-responsive-100-width-youtube-iframe-embed

like image 25
Erik Hopkins Avatar answered Sep 26 '22 05:09

Erik Hopkins