html - 嵌入 youtube 视频高度问题
问题描述
我正在尝试将 youtube 视频嵌入到我的项目中,它可以正常工作,并且我还尝试padding-bottom: 56.25%
像大多数教程一样应用 16:9 的比例。
问题是,视频的高度恰好是容器的 100%,我应该在我的 CSS 中修复什么来摆脱顶部/底部黑条?
这是我的代码笔尝试:https ://codepen.io/DieByMacro/pen/QXmJez
.HomePage-homeVideoWrapper-274 {
height: 0;
margin: auto;
z-index: 1;
position: relative;
max-width: 720px;
padding-top: 25px;
padding-bottom: 56.25%;
}
.HomePage-homeVideoWrapper-274 iframe {
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 5;
position: absolute;
}
<div class="HomePage-homeVideoWrapper-274">
<iframe height="auto" src="https://www.youtube.com/embed/Qjmp2r2OsZ4" title="Home-admin tutorial" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"></iframe>
</div>
解决方案
这是我们在模板上添加视频时面临的非常常见的问题。您只需在OUTER div 上添加 max-width 和 width 属性,而不是在HomePage-homeVideoWrapper-274上。无需在 HomePage-homeVideoWrapper-274 中添加max-height 。
.outer {
width: 100%;
max-width: 750px;
margin: 0 auto;
}
.HomePage-homeVideoWrapper-274 {
height: 0;
margin: auto;
z-index: 1;
position: relative;
padding-top: 25px;
padding-bottom: 56.25%;
display: block;
overflow: hidden;
iframe {
display: block;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 5;
position: absolute;
}
}
<div class="outer">
<div class="HomePage-homeVideoWrapper-274">
<iframe src="https://www.youtube.com/embed/Qjmp2r2OsZ4" title="Home-admin tutorial"></iframe>
</div>
</div>
推荐阅读
- html - minlength 属性似乎不起作用
- python - max() 函数如何在以下代码中工作
- javascript - 如何将整个数组添加到 useState 变量
- sql - SQL database_id 值是否被重用?
- python - random.random() 在不同的机器上生成不同的数字
- scala - 哪些操作在流库中的拉取模型和推送模型中更容易实现(反之亦然)?
- r - Google Cloud Compute Engine 上的 xgboost 超慢
- c# - 如何使用 C# 和 Google 日历 API 创建 Google 日历活动?
- c# - ExecuteAsync 更改传递的动态参数名称
- python - 在 VS Code 中无法切换到 Python3