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

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