【问题标题】:Vertically center responsive iframe垂直居中响应式 iframe
【发布时间】:2014-03-05 21:36:32
【问题描述】:

我正在使用described here 技术使 iframe(视频)具有响应性。本质上,iframe 绝对定位在 100% 宽度的包装元素内。包装元素根据视频的纵横比设置填充:

.embed-responsive {
    position: relative;
    /* video height / video width */
    padding-bottom: 56.2%;
    height: 0;
    overflow: hidden;
}
.embed-responsive iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

我需要能够将 iframe 在父容器(高度为 100%)中垂直居中。

我在使用 this technique 之前已经实现了这一点,但它只适用于 inline-block 元素。如果我将 .embed-responsive 更改为 inline-block 它会中断。

那么有没有一种替代技术(最好只使用 CSS)我可以用来垂直居中 iframe,同时仍然保持响应?这必须在调整浏览器大小时起作用。

http://jsfiddle.net/benfosterdev/xfA3L/

【问题讨论】:

    标签: css iframe responsive-design


    【解决方案1】:

    所以我在发布 2 分钟后发现了这一点 :)

    使.embed-resposive 绝对定位,top:50% 和顶部边距设置为高度比的一半(video height / video width) / 2 我可以垂直居中:

    .embed-responsive {
        position: absolute;
        left: 0; right: 0;
        top: 50%;
        /* video height / video width */
        padding-bottom: 56.2%;
        /* the above value * 0.5 */
        margin-top: -28.1%;
        height: 0;
        overflow: hidden;
    }
    

    Fiddle 已更新。

    【讨论】:

    • 您应该提到高度为 100% 的封闭容器 div,如您的问题的小提琴示例所示,我发现让它工作至关重要。
    • 这使我的视频与整个窗口一样大,并在其下方放置空白空间:(
    猜你喜欢
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 2015-01-03
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多