2

I am new to coding and community and i am trying to embed YouTube iframes responsively. So far all i do is replacing the original width value with 100%.

 <iframe width="100%" height="315" src="https://www.youtube.com/embed/kXBunIe_PSw" frameborder="0" allowfullscreen></iframe>

Yes it makes it responsive since the height is static there are lots of black space. Is there any simple way to prevent this.

I prefer css solutions but javascript is also welcomed.

Aslam
  • 9,204
  • 4
  • 35
  • 51

2 Answers2

0

.videoWrapper {
 position:relative;
 padding-bottom:56.25%;
 margin:auto;
 height:0;
}
.videoWrapper iframe {
 background-color:#f2f2f2;
 position:absolute;
 max-width:100%;
 max-height:315px;
 width:95%;
 height:95%;
 left:0;
 right:0;
 margin:auto;
}
    <div class='videoWrapper'>
      <iframe id='video' class='trailervideo' width='560' height='315' src='https://www.youtube.com/embed/hhR3DwzV2eA' src='about:blank' frameborder='0' allowfullscreen></iframe>
    </div>

Let me know, if it does not work

Kamal Chhirang
  • 490
  • 4
  • 14
0

This should help you get started.

Ref: Bootstrap

.responsive {
  position: relative;
  display: block;
  height: 0;
  padding: 0;
  overflow: hidden;
}

.responsive iframe {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  height: 100%;
  width: 100%;
  border: 0;
}

.video {
  padding-bottom: 56.25%;  /* or 75% depending upon the type of video you have */
}
<div class="responsive video">

  <iframe src="https://www.youtube.com/embed/kXBunIe_PSw" allowfullscreen></iframe>

</div>
Aslam
  • 9,204
  • 4
  • 35
  • 51