CSS - 100%宽度的Youtube嵌入视频

问题描述 投票:0回答:3

HTML:

<div class="video">
  <iframe src="https://www.youtube.com/embed/bRhZUF47p2E?version=3&amp;rel=0&amp;controls=0&amp;showinfo=0&amp;autoplay=1&amp;mute=1&amp;loop=1" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>

CSS:

.video {
  width: 100%;
  height: 200px;
  border: 1px solid red;
  overflow: hidden;
  position: relative;
}
iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

的jsfiddle:http://jsfiddle.net/pj2utq4v/

问题:如何强制iframe为父div宽度的100%。由于父div的高度只有200像素,iframe视频会被裁剪,这对我也没问题。

html css youtube youtube-iframe-api
3个回答
1
投票

你可以这样做一点改变你的CSS类:

.video {
    position: relative;
    width: 100%;
    height: 0;
    padding-bottom: 56.25%;
}

iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

的jsfiddle:http://jsfiddle.net/pj2utq4v/1


0
投票

你有没有理由想使用iframe?我会尝试使用HTML5视频标签。这是一个例子:https://www.w3schools.com/html/html5_video.asp然后将你的视频宽度设置为100%,如下所示:

<video width="100%" controls>
  <source src="mov_bbb.mp4" type="video/mp4">
  <source src="mov_bbb.ogg" type="video/ogg">
  Your browser does not support HTML5 video.
</video>

0
投票

添加了一个溢出隐藏和固定高度的div,它的工作原理。

<div style="height: 200px; overflow: hidden">
  <div class="video">
    <iframe src="https://www.youtube.com/embed/bRhZUF47p2E?version=3&amp;rel=0&amp;controls=0&amp;showinfo=0&amp;autoplay=1&amp;mute=1&amp;loop=1" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
  </div>
</div>
<style>
.video {
  width: 100%;
  height: 200px;
  border: 1px solid red;
  overflow: hidden;
  position: relative;
}
iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
</style>

http://jsfiddle.net/pj2utq4v/

© www.soinside.com 2019 - 2024. All rights reserved.