像图像一样缩放iFrame css宽度100%

Oli*_*ver 52 css iframe responsive-design

我想通过CSS缩放iFrame width: 100%,高度应按比例缩放到宽度.

使用<img>标签,这工作正常.

图像和iFrame都在html中定义了宽度和高度.

这里有一些例子:

<html>
    <style>
        #a{ width: 500px; }
        img{ width: 100%; height: auto }
    </style>
    <body>
        <div id="a">
            <img src="http://lorempixel.com/200/150/" width="200" height="150" />
        </div>
    </body>
Run Code Online (Sandbox Code Playgroud)

这对图像效果很好,但我想对iFrames采取相同的行为:

<html>
    <style>
        #a{ width: 900px; background: grey;}
        iframe{ width: 100%; height: auto }
    </style>
    <body>
        <div id="a">
            <iframe width="560" height="315" src="http://www.youtube.com/embed/RksyMaJiD8Y" frameborder="0" allowfullscreen></iframe>
        </div>
    </body>
Run Code Online (Sandbox Code Playgroud)

iFrame渲染100%宽,但不像图像那样按比例缩放高度.

Rik*_*Rik 111

图像和iframe之间的巨大差异在于图像保持其宽高比.您可以将图片和iframe合并,从而生成响应式iframe.希望这回答你的问题.

请查看此链接,例如:http://jsfiddle.net/Masau/7WRHM/

HTML:

<div class="wrapper">
    <div class="h_iframe">
        <!-- a transparent image is preferable -->
        <img class="ratio" src="http://placehold.it/16x9"/>
        <iframe src="http://www.youtube.com/embed/WsFWhL4Y84Y" frameborder="0" allowfullscreen></iframe>
    </div>
    <p>Please scale the "result" window to notice the effect.</p>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

html,body        {height:100%;}
.wrapper         {width:80%;height:100%;margin:0 auto;background:#CCC}
.h_iframe        {position:relative;}
.h_iframe .ratio {display:block;width:100%;height:auto;}
.h_iframe iframe {position:absolute;top:0;left:0;width:100%; height:100%;}
Run Code Online (Sandbox Code Playgroud)

注意:这仅适用于固定的宽高比.

  • 还有一点需要注意,图像不是必需的(至少在现代浏览器中).padding-top(设置填充到宽高比)技巧有效:<div style ="width:100%; padding-top:75.4%"> <iframe style ="position:absolute; width:100%; height:100% ; top:0; left:0;"></iframe> </ iframe> </ div> (15认同)
  • @ansiart如果你需要设置`max-width`,你的填充顶部技巧不起作用.而且,你忘记了容器上的`position:relative`. (3认同)
  • 呃..我们过去使用透明图像进行布局,保持滚动,因为Simone Lazzari的答案是优越的! (2认同)

Sim*_*ari 56

@Rik,我想这是一种更清洁的方法.它适用于内联'高度'和'宽度'属性(我在小提琴中设置随机值来证明这一点)和CSS'max-width'属性(@Walf你读过我吗?).

HTML:

<div class="wrapper">
    <div class="h_iframe">
        <iframe height="2" width="2" src="http://www.youtube.com/embed/WsFWhL4Y84Y" frameborder="0" allowfullscreen></iframe>
    </div>
    <p>Please scale the "result" window to notice the effect.</p>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

html,body        {height: 100%;}
.wrapper         {width: 80%; max-width: 600px; height: 100%; margin: 0 auto; background: #CCC}
.h_iframe        {position: relative; padding-top: 56%;}
.h_iframe iframe {position: absolute; top: 0; left: 0; width: 100%; height: 100%;}
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/7WRHM/1001/

  • 我希望为这个答案增加更多的可见性,因为它似乎是一个明显的赢家.之前的回答涉及添加一个隐藏的img来触发扩展是很聪明的,但强制额外的http请求,特别是在移动设备上,远非理想.这个纯CSS的答案是一个更好的方法. (9认同)
  • @Simone,偏离主题但是我个人更喜欢css唯一的方法.一个好的读物是:http://alistapart.com/article/creating-intrinsic-ratios-for-video对于IE7支持,请查看http://alistapart.com/d/creating-intrinsic-ratios-for-video/example4. HTML (2认同)

Ana*_*ist 14

我最喜欢这个解决方案.简单,可扩展,响应迅速.这里的想法是创建一个零高度外部div,底部填充设置为视频的宽高比.iframe的宽度和高度均缩放至100%,完全填充外部容器.外部容器根据其宽度自动调整其高度,内部的iframe相应地调整自身.

<div style="position:relative; width:100%; height:0px; padding-bottom:56.25%;">
    <iframe style="position:absolute; left:0; top:0; width:100%; height:100%"
        src="http://www.youtube.com/embed/RksyMaJiD8Y">
    </iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

这里唯一的变量是外部div中的padding-bottom值.4:3宽高比视频为75%,宽屏16:9宽高比视频为56.25%.


Dan*_*eld 7

您可以在此处使用视口单位而不是 %。像这样:

iframe {
    max-width: 100vw;
    max-height: 56.25vw; /* height/width ratio = 315/560 = .5625 */
}
Run Code Online (Sandbox Code Playgroud)

DEMO(调整大小看效果)

iframe {
    max-width: 100vw;
    max-height: 56.25vw; /* height/width ratio = 315/560 = .5625 */
}
Run Code Online (Sandbox Code Playgroud)
body {
  margin: 0;
}
.a {
  max-width: 560px;
  background: grey;
}
img {
  width: 100%;
  height: auto
}
iframe {
  max-width: 100vw;
  max-height: 56.25vw;
  /* 315/560 = .5625 */
}
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案非常棒,因为它不需要 HTML 有任何额外的标签,也不涉及 JavaScript。 (2认同)