css - 带有 iframe 的响应式 youtube 视频

标签 css

我在 html 中有一个以下 iframe,其中包含 youtube 视频。这是 iframe:

<iframe width="420" height="315" src="https://www.youtube.com/embed/DgPO56ImqUA?showinfo=1" frameborder="0" allowfullscreen></iframe>

我怎样才能使这个 iframe 响应?

最佳答案

您可以使用固有比率。包含元素是响应式的,并保持特定的纵横比。里面的iframe绝对定位到这个元素。

HTML

<div class="videoWrapper">
    <!-- Copy & Pasted from YouTube -->
    <iframe width="420" height="315" src="https://www.youtube.com/embed/DgPO56ImqUA?showinfo=1" frameborder="0" allowfullscreen></iframe>
</div>

CSS

.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%;
}

有关 CSS Tricks 的更多信息

关于css - 带有 iframe 的响应式 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30529820/

相关文章:

javascript - 防止从周围激活菜单

css - 如果页面不是 "Warnings",是否应该认真对待 FF 错误控制台 "broken"?

html - 使 flex 元素重叠

CSS 关键帧 : multiple frames smooth transitions

html - body 下方的空白空间和 div 的奇怪位置

html - 为什么在这个例子中 <ul> <li> 和 <a> 元素的高度不同?

html - CSS 6 列设置问题

css - Google Material Design ALL 24 Elevations

javascript - 如何在CSS中模拟真实世界的尺寸?

css -::before/::after + MDN 中的 flexbox