CSS - 100% width of Youtube embed video
Asked Answered
J

3

10

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/

QUESTION: How to force iframe to be 100% of parent div width. Since parent div is only 200px in height, iframe video would be cropped which is also okay with me.

Jamila answered 18/9, 2018 at 20:1 Comment(0)
J
0

Added one more div with overflow hidden and fixed height and it works.

<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/

Jamila answered 18/9, 2018 at 20:43 Comment(1)
How to set aspect ratio for this?Reproof
S
11

You can do this changing your CSS classes a little bit:

.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

Sg answered 18/9, 2018 at 20:34 Comment(1)
I don't want to have proportional aspect ratio. Parent div must be 200px.Jamila
J
0

Added one more div with overflow hidden and fixed height and it works.

<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/

Jamila answered 18/9, 2018 at 20:43 Comment(1)
How to set aspect ratio for this?Reproof
M
-5

Is there a reason you want to use iframes? I would try using HTML5 video tag. Here's an example: https://www.w3schools.com/html/html5_video.asp then set your video width to 100% like this:

<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>
Mccafferty answered 18/9, 2018 at 20:27 Comment(2)
Actually I want to avoid bandwidth burn.Jamila
He probably uses iFrame because that's the way to embed youtube video...Sauer

© 2022 - 2024 — McMap. All rights reserved.