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

后端 未结 2 893
萌比男神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/

    0 讨论(0)
  • 2021-02-06 20:42

    braican is correct, but the 56.25% on the video container will leave lots of padding after your video. Just wrap everything inside another div with a max-height of 320px and overflow:hidden to hide the extra padding;

    0 讨论(0)
提交回复
热议问题