Fullscreen background video and keep it centered

前端 未结 4 445
梦谈多话
梦谈多话 2021-01-11 17:13

I\'m trying to create a site where I have a background video playing with some HTML5. This is all working perfectly, it works just the way I want it. But I also want to keep

相关标签:
4条回答
  • 2021-01-11 17:35

    Use Css Property. object-fit: cover;

    body, html {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
    
    video {
      height: 100%;
      width: 100%;
    }
    <video controls> 
      <source src=http://techslides.com/demos/sample-videos/small.webm type=video/webm> 
      <source src=http://techslides.com/demos/sample-videos/small.ogv type=video/ogg> 
      <source src=http://techslides.com/demos/sample-videos/small.mp4 type=video/mp4>
      <source src=http://techslides.com/demos/sample-videos/small.3gp type=video/3gp>
    </video>

    0 讨论(0)
  • 2021-01-11 17:40

    I would try centering the video with position absolute inside of a fixed wrapper. So for example:

    Place your video inside of a fixed wrapper with 100% width and height:

    #video-wrap {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
    }
    

    Center the video inside of an extra large area with margin auto:

    #video {
        position: absolute;
        top: -9999px;
        bottom: -9999px;
        left: -9999px;
        right: -9999px;
        margin: auto;
    }
    

    And stretch it to full size with min-width and min-height:

    #video {
        min-width: 100%;
        min-height: 100%;
        width: auto;
        height: auto;
    } 
    

    Here the final result:

    #video-wrap {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
    }
    #video {
        position: absolute;
        top: -9999px;
        bottom: -9999px;
        left: -9999px;
        right: -9999px;
        margin: auto;
        min-width: 100%;
        min-height: 100%;
        width: auto;
        height: auto;   
    }
    <div id="video-wrap">
        <video id="video" loop autoplay>
            <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
            Your browser does not support HTML5 video.
        </video>
    </div>

    Here also a jsfiddle.

    0 讨论(0)
  • 2021-01-11 17:45

    This should make #video the entire size of the viewport and remain there when the user scrolls.

    #video {
       position: fixed;
       top: 0;
       right: 0;
       bottom: 0;
       left: 0;
    }
    
    0 讨论(0)
  • 2021-01-11 18:00

    This question just have been referenced into Place video with 100% height & 100% width using css or javascript

    I guess my answer for that could be the one you were looking for?

    Here's the code:

    header {
        position: relative;
        height: 100vh;
        z-index: 0;
    }
    header h1 {
        text-align: center;
        font: 3em/1.4 helvetica neue, helvetica, arial, sans-serif;
        color: #fff
    }
    header video {
        -webkit-transform: translateX(-50%) translateY(-50%);
        -moz-transform: translateX(-50%) translateY(-50%);
        -ms-transform: translateX(-50%) translateY(-50%);
        -o-transform: translateX(-50%) translateY(-50%);
        transform: translateX(-50%) translateY(-50%);
        position: absolute;
        top: 50%;
        left: 50%;
        min-width: 100%;
        min-height: 100%;
        width: auto;
        height: auto;
        z-index: -100;
    }
    <header>
        <h1>Sample Title</h1>
    	<video autoplay loop class="bg-video">
    		<source src="https://d2v9y0dukr6mq2.cloudfront.net/video/preview/abstract-rainbow_wjpctkdes__PM.mp4" type="video/mp4">
    	</video>
    </header>

    And here's a working fiddle example.

    Hope it'll help someone else :)

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