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

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

    Old question, but I think the @media CSS 3 tags would be helpful in this instance.

    Here is my solution to a similar problem.

    The CSS:

    @media only screen and (min-width: 769px) {
        .yVid {
            width: 640px;
            height: 360px;
            margin: 0 auto;
        }
    }
    
    @media only screen and (max-width: 768px) {
        .yVid {
            width: 560px;
            height: 315px;
            margin: 0 auto;
        }
    }
    

    The HTML:

    This basically adds a breakpoint at 768px where the video resizes itself. You could also add breakpoints at 992 and 1280 for an even more repsonsive video size. (numbers based on Bootstrap standard sizes).

提交回复
热议问题