Responsive fullscreen youtube video with no black bars?

后端 未结 8 1802
执笔经年
执笔经年 2020-12-08 10:20

I have a fullscreen youtube video embedded on my website.

\"enter

It looks goo

8条回答
  •  情书的邮戳
    2020-12-08 11:03

    To simulate the same effect, the important thing is to maintain aspect ratio which is 16:9.

    HTML

    
    

    CSS

    iframe{
      width: 100%;
      height: calc((100vw*9) /16);
    }
    

    This will remove black bars.

    Now we can set the outer container to 100% width and 100% height and hide the overflow.

    .banner-video{
      height: 100vh;
      overflow: hidden;
    }
    

    Now the above code will work until viewport aspect ratio is greater than 16/9. So we have to add media query based on aspect ratio.

     @media (max-aspect-ratio: 16/9) {
        .banner-video{
          width: 100%;
          overflow: hidden;
        }
    
        iframe{
          width: calc((100vh*16)/9);
          height: 100vh;
          }
      }
    

    After this youtube video will maintain full viewport size at all condition and hide the extra part of a video. Other then opera it's supported in all the browser.

提交回复
热议问题