How can I make youtube video responsive along with background image?

后端 未结 2 892
萌比男神i
萌比男神i 2021-02-06 19:45

I have responsive background and I want to have a YouTube video over that background(not in full width).

Here I have tried doing it.

http://jsfiddle.net/audetwe

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 20:24

    Here's a jsfiddle forked from your fiddle that has the image as the background, as well as a responsive youtube video centered. Making the image have position:absolute takes it out of the normal flow and allows the embedded video to stay on top.

    The trick for the responsive video code is to wrap the embedded video in a container with a max width, and then also adding in padding to keep the proper aspect ratio for the video. You then ensure that the iframe, object, and embded elements all fit at 100% of that container's width while also not getting any taller than the native size:

    .video-container {
        position: relative;
        padding-bottom: 56.25%;
        padding-top: 30px;
        height: 0;
        overflow: hidden;
        width: 100%;
        max-width: 560px;
        margin: 0 auto;
    }
    .video-container iframe,
    .video-container object,
    .video-container embed {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    
        max-height: 320px;
    }
    

    http://jsfiddle.net/QRkL9/

    More about the above code - http://avexdesigns.com/responsive-youtube-embed/

提交回复
热议问题