Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying a YouTube video with iframe full width of page [closed]

I've put a video in my site using an iframe from YouTube, while the iframe is full width (100%) the video is very small (height) within the frame. How do I make it fit the width of the container?

Code:

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

See screenshot

enter image description here

like image 959
user2953989 Avatar asked Jul 08 '16 15:07

user2953989


People also ask

How do you embed full width on youtube?

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 do I get youtube to allow full screen in iframe?

Try adding the allowfullscreen attribute to the "parent" iframe. you only need <iframe src="url" frameborder="0" allowfullscreen></iframe>, some other attributes are deprecated.

How do I resize an iframe on youtube?

You have to use responsive styling. For this you need an extra div as wrapper. With next html and css, the youtube will be as wide as the available space (in this css 90% of the available space). Ofcourse both width and height will resize to maintain the 16:9 ratio of an youtube movie.

How can I get 100 iframe width?

To get the iframe to properly use 100% the parent needs to be 100%. In newer doctypes the html and body tag are not automatically 100%. When I added height:100% for html and body then it worked flawlessly.


2 Answers

To make a You Tube Video full width and preserve the height (and keep it responsive) you can use the following setup.

.videoWrapper {   position: relative;   padding-bottom: 56.25%;   /* 16:9 */   padding-top: 25px;   height: 0; }  .videoWrapper iframe {   position: absolute;   top: 0;   left: 0;   width: 100%;   height: 100%; }
<div class="videoWrapper">   <!-- Copy & Pasted from YouTube -->   <iframe width="560" height="349" src="https://www.youtube.com/embed/n_dZNLr2cME?rel=0&hd=1" frameborder="0" allowfullscreen></iframe> </div>
like image 83
Web Develop Wolf Avatar answered Sep 22 '22 04:09

Web Develop Wolf


The issue is that an iframe and video can't scale like an image, where it gets the proportionally correct height based on the aspect ratio if you just leave the height at the default.

A solution for this is to figure out the native width/height of your video, and do a little math to figure out what the height should be at 100% screen width. Assuming it's 1920x1080, you can do something like this using viewport width (vw). You can reduce the 100vw to whatever fits better, if you don't want it to literally fill the screen.

iframe{  width: 100vw  height: calc(100vw/1.77); } 
like image 34
will Avatar answered Sep 21 '22 04:09

will