html - Youtube 视频 100% 宽度和自定义高度

标签 html css iframe youtube

我想在我的网站上添加一个宽度为 100%、高度为 400px 的视频;我尝试了以下方法:

.container {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 56.25%;
}
.video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100% !important;
  height: 400px;
}
<div class="container">
  <iframe class="video" src="//www.youtube.com/embed/yCOY82UdFrw" width="300" height="150" frameborder="0" allowfullscreen="allowfullscreen">
  </iframe>
</div>

但我的视频结果是这样的: click me pls

内部不是 100%。

有没有人知道如何解决这个问题?我将不胜感激。

谢谢,

凯文

最佳答案

请记住,百分比宽度或高度必须有一个明确声明了某种尺寸的父元素。在这种情况下是:

html, body {width:100%;高度:100%;

对于高度,您仍然可以灵活地使用 400px:

height: auto;
min-height: 400px;

片段

html,
body {
  height: 100%;
  width: 100%;
}
.container {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 56.25%;
}
.video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100% !important;
  height: auto;
  min-height: 400px;
  overflow: hidden;
}
<div class="container">
  <iframe class="video" src="//www.youtube.com/embed/yCOY82UdFrw" frameborder="0" allowfullscreen="allowfullscreen">
  </iframe>
</div>

关于html - Youtube 视频 100% 宽度和自定义高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39465395/

相关文章:

python - 127.0.0.1 拒绝连接

html - 如何为显示编码为 : table-cell? 的多个内联 DIV 强制设置最大 DIV 高度

css - Wordpress 内联图像 - 它会忽略右边距值吗?

javascript - 如何使用 svg "focusable"制作 iframe

c# - HTML Razor 中 Step 属性的自定义错误验证

javascript - 如何调整图像大小以适应按下按钮时的容器高度?

html - 如何使 <li> 标签上的背景与下拉菜单相同

javascript - 如何让 Toggle button JS 继续工作?

javascript - 如何列出 iframe 请求的资源

jquery - 使用位于 iFrame 内的 jQuery 播放视频