【发布时间】:2018-02-07 13:48:08
【问题描述】:
我正在创建 YouTube 提供视频源的相对(非固定)视频背景。
主父容器的最大高度为 600 像素,但这样做会切断 YouTube 视频的底部。有没有办法将 600px 的最大高度也应用到 iframe,但保持全宽和响应式?
HTML
<div id="video-container">
<div class='embed-container'>
<iframe src='https://www.youtube.com/embed/YOPnzvSrZd4?mute=1&controls=0&showinfo=0&rel=0&autoplay=1&loop=1&playlist=YOPnzvSrZd4&start=0' frameborder='0' allowfullscreen>
</iframe>
</div>
<div class="video-area">
<div class="video-table">
<div class="video-cell">{video-area:content}</div>
</div>
</div>
</div>
CSS
#video-container {
width: 100%;
overflow: hidden;
position: relative;
max-height: 600px;
}
.video-area {
position:absolute;
top: 0;
width: 100%;
height: 100%;
}
.video-table {
display:table;
width:100%;
height:100%;
max-width: 960px;
margin: 0 auto;
}
.video-cell {
display:table-cell;
vertical-align:middle;
padding:2em;
box-sizing: border-box;
}
.embed-container {
position: relative;
padding-bottom: 56.25%;
height: 0;
overflow: hidden;
max-width: 100%;
}
.embed-container iframe,
.embed-container object,
.embed-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
【问题讨论】:
-
我想你是在问如何保持视频的正确纵横比,但不要让它超过 600 像素。当您使用宽屏幕时,视频将居中但保持宽高比,在较小的屏幕上您将获得全宽视频,因为高度小于 600 像素。这看起来对吗?代码中的视频是您尝试调整的实际视频,还是与预期视频的宽高比相同,或者您确定它是 4:3、16:9 还是其他?
-
没错!理想情况下,它会创建与 background-size:cover 和 background-position: 50% 50% 的图像相同的结果。如果可能的话,我想考虑所有类型的纵横比,但如果没有,上面的视频就可以了。
-
酷。你可以做到,我或其他人稍后会发布答案,但需要根据纵横比调整样式。不幸的是,它不会像 img 标签或背景大小包含的那样容易。技巧涉及 0 高度和百分比的顶部或底部填充子元素。
-
你就是那个人,@JasonB!非常感谢您花时间帮助我解决这个问题。
-
如果您在
iframe中使用max-height: 600px的同时还使用height: 100%,视频将根据屏幕的width很好地调整,而不会剪切视频。你试过@Weebs 吗?