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

后端 未结 14 1963
一向
一向 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:

    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;
        }       
    
    }       
    

提交回复
热议问题