How can I make the YouTube player scale to the width of the page but also keep the aspect ratio?

后端 未结 14 1924
一向
一向 2021-01-30 11:09

I have a YouTube video I want to put on my web page.

I want to scale the video to fit to a percent of the users browser but also to keep the aspect ratio.

I hav

相关标签:
14条回答
  • 2021-01-30 11:21

    In addition to Darwin and Todd the following solution will

    1. avoid the bottom margin
    2. maximize the width for large screens
    3. minimize the height in mobile view
    4. keep a fixed size for @media none compatible browsers

    The HTML:

    <div class="video_player">
      <div class="auto-resizable-iframe">
        <div>
          <iframe frameborder="0" allowfullscreen="" src="http://www.youtube.com/embed/_OBlgSz8sSM">        </iframe>
        </div>
      </div>
    </div>
    

    The CSS:

    .videoplayer{
    
        text-align: center;
        vertical-align: middle;
    
        background-color:#000000;
    
        margin: 0;
        padding: 0;
    
        width: 100%;
        height:420px;
    
        overflow:hidden;
    
        top: 0;
        bottom: 0;
    
    }
    
    .auto-resizable-iframe {
        width:100%;
        max-width:100%;
        margin: 0px auto;
    }
    
    .auto-resizable-iframe > div {
        position: relative;
        padding-bottom:420px;
        height: 0px;
    }
    
    .auto-resizable-iframe iframe {
        position: absolute;
        top: 0px;
        left: 0px;
        width: 100%;
        height: 100%;
    }
    
    
    //full screen
    @media (min-width:0px) {
    
        .videoplayer{
            height:100%;
        }
    
        .auto-resizable-iframe > div {
            padding-bottom:100%;
        }           
    
    }
    
    //mobile/pad view
    @media (min-width:600px) {
    
        .videoplayer{
            height:420px;
        }
    
        .auto-resizable-iframe > div {
            padding-bottom:420px;
        }       
    
    }       
    
    0 讨论(0)
  • 2021-01-30 11:23

    The trick to make a youtube video autoresize is to make the iframe width 100% and put it in a div with a "padding-bottom" equal to the aspect ratio in percentage. E.g.

    But the problem is - you would have a lot of pages with embedded YoutTube videos already. Here's a jquery plugin that will scan all videos on the page and make them resizable automatically by changing the iframe code to be as above. That means you don't have to change any code. Include the javascript and all your YouTube videos become autoresizing. https://skipser.googlecode.com/files/youtube-autoresizer.js

    0 讨论(0)
  • 2021-01-30 11:23

    This is what worked for me. This is slightly modified code from the YouTube Embed Code Generator.

    The CSS:

    .video-container {
        position: relative;
        width: 100%;
        height: 0;
        padding-bottom: 56.27198%;
        }
    .video-container iframe {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        }
    

    The HTML:

    <div class="video-container">
        <iframe width="560px" height="315px" src="https://www.youtube.com/embed/XXXXxxxx?&theme=dark&autohide=2&iv_load_policy=3"><iframe>
    </div>
    
    0 讨论(0)
  • 2021-01-30 11:24

    I hit a similar issue with my site when developing some responsive CSS. I wanted any embedded Youtube objects to resize, with aspect, when switching from the desktop CSS to something smaller (I use media queries to re-render content for mobile devices).

    The solution I settled on was CSS and mark-up based. Basically, I have three video classes in my CSS thus:

    .video640 {width: 640px; height: 385px}
    .video560 {width: 560px; height: 340px}
    .video480 {width: 480px; height: 385px}
    

    … and I assign one of these to the Youtube content I include, depending on its original size (you may need more classes, I just picked the most common sizes).

    In the media query CSS for smaller devices, these same classes are simply re-stated like so:

    .video640 {width: 230px; height: 197px}
    .video560 {width: 230px; height: 170px}
    .video480 {width: 240px; height: 193px}
    

    I appreciate this requires some mark-up "up-front" when including videos in your HTML (i.e. adding a class), but if you don't want to go down the Javascript route, this works pretty well -- you could re-state your video classes for as many different sizes as you require. Here's how the Youtube mark-up looks:

    <object class="video640" type="application/x-shockwave-flash" value="YOUTUBE URL">
      <param name="movie" value="YOUTUBE URL"></param>
    </object>
    
    0 讨论(0)
  • 2021-01-30 11:24

    Just set iframe height and width with CSS vw metric. It uses device width as parameter:

    .videoWrapper iframe {
        height: 36.6vw;
        width: 65vw; 
    }
    
    0 讨论(0)
  • 2021-01-30 11:26

    You can use style="max-width: %87; max-height: %87;"

    0 讨论(0)
提交回复
热议问题