Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - 100% width of Youtube embed video

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.

like image 688
RhymeGuy Avatar asked Sep 18 '18 20:09

RhymeGuy


People also ask

How do you embed a YouTube video in 100 width?

You will need to wrap the responsive youtube embed code with a div and specify a 50% to 60% padding bottom. Then specify the child elements (iframe, object embed) 100% width, 100% height, with absolute position. This will force the embed elements to expand fullwidth automatically.

How can I get 100 iframe width?

To size the <iframe> , we ignore the width="560" height="315" element properties, and instead use the CSS properties width:100%;height:100%; . That's it: a full-width <iframe> with fixed aspect ratio.


1 Answers

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

like image 95
Gonzalo Santiago Avatar answered Sep 17 '22 17:09

Gonzalo Santiago