Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed YouTube IFrame responsive

I embedded YouTube video with the following link on a Magento site (Magento is not really important unless there is a plugin that I am not aware of)

<iframe width="640" height="360" src="http://www.youtube.com/embed/Zq-805aUM7M?feature=player_embedded" frameborder="0" allowfullscreen></iframe>

I don't believe this piece of code is good because it is not responsive. How can I fix?

like image 465
user829174 Avatar asked Jul 19 '15 15:07

user829174


People also ask

Can an iframe be responsive?

Unlike images and the native HTML5 video element, iframes do not scale responsively by default.

How do I embed an iframe on YouTube?

On a computer, go to the YouTube video or playlist you want to embed. From the list of Share options, click Embed. From the box that appears, copy the HTML code. Paste the code into your website HTML.


2 Answers

try this pure css way:

iframe, object, embed {
        max-width: 100%;
        max-height: 100%;
}

if that does not work try this

https://benmarshall.me/responsive-iframes/

like image 123
Josh Stevens Avatar answered Sep 30 '22 19:09

Josh Stevens


To specifically target youtube videos, and not all iframes or objects of embeds, you can use attribute selector based on string present in src.

iframe[src*=youtube]
{
  max-width:100%;
  height:100%;
}

This works for me, but anyway, here you can find more solutions for additional (specific) cases

like image 25
Luca Detomi Avatar answered Sep 30 '22 18:09

Luca Detomi