Html5 video background, keep center of video in center

后端 未结 10 1358
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 12:00

I am trying to keep a background video centered regardless of how big the user drags the video. It\'s currently cutting off the right side of the videos when i scroll smalle

相关标签:
10条回答
  • 2020-12-23 12:11

    just center it like any other element with position absolute

    .video_contain {
        position: absolute;
        width: auto;
        min-height: 100%;
        min-width: 100%;
        left: 50%;
        transform: translate(-50%);
    }
    
    0 讨论(0)
  • 2020-12-23 12:12

    This did the trick for me, keeping the video centered all the time and not worrying about the actual dimensions of the video

    .video_contain {
      position: absolute;
      top: 0;
      left: 0;
      /** could be any size **/
      width: 100%;
      height: 100%;
      overflow: hidden;
      z-index: 0;
    }
    video {
      display: block;
      min-height: 100%;
      min-width: 100%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      z-index: 1;
    }
    
    0 讨论(0)
  • 2020-12-23 12:13
      .bg-video-wrap {
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
      }
    
      .bg-video-wrap > video,
      .bg-video-wrap > iframe {
        object-fit: cover;
        object-position: center center;
        width: 100%;
        height: 100%;
      }
    
    0 讨论(0)
  • 2020-12-23 12:14

    here's how I typically do background video, and how I did it for the stre.am landing page:

    .video_contain {
        position: absolute;
        top: -50%;
        left: -50%;
        width: 200%;
        height: 200%;
    }
    
    video {
        position: absolute;
        top: 0;
        bottom: 0;
        right: 0;
        left: 0;
        margin: auto;
        min-height: 50%;
        min-width: 50%;
    }
    

    stream fs vid for mobile

    0 讨论(0)
  • 2020-12-23 12:20

    Late to the party but I wanted to give a 2020 answer. Here's a simple solution that lets you have an HTML video both centered and responsive without being "fixed" positioned. It lets you start with a fullscreen intro and add some text right when you start scrolling. No scrollbars, no annoying things. As simple as that.

    https://codepen.io/LuBre/pen/GRJVMqE?editors=1100

    CSS

    * {
      box-sizing: border-box;
    }
    body, html {
      margin: 0;
      height: 100%;
      font-Family: Arial;
    }
    
    .video-container {
      display: grid;
      justify-items: center;
      align-items: center;
      position: relative;
      width: 100%;
      height: 100%;
      overflow: hidden;
    }
    .video-container video {
      position: absolute;
      z-index: 1;
      top: 50%;
      left:50%;
      min-width: 100%;
      min-height: 100%;
      transform: translateX(-50%) translateY(-50%);
    }
    .video-text {
      z-index: 2; 
      color: #fff;
      text-align: center;
    }
    .video-container h1, .video-container h2  {
      margin: 0;
      font-size: 3rem;
    }
    .video-container h2  {
      font-size: 1.4rem;
      font-weight: normal;
      opacity: 0.6;
    }
    
    .page-content {
      line-height: 1.4rem;
      padding: 2rem;
    }
    

    HTML

    <div class="video-container">
      <video autoplay muted loop>
        <source src="https://www.w3schools.com/howto/rain.mp4" type="video/mp4">
      </video>
      <div class="video-text">
        <h1>Catchy title</h1>
        <h2>Everyone loves catchy titles</h2>
      </div>
    </div>
    
    <div class="page-content">
    <h1>New paragaph</h1>
    Some random text goes here...
    
    0 讨论(0)
  • 2020-12-23 12:27

    This worked for me

    .video_contain {
      position: absolute;
      z-index: -1;
      top: 0px;
      left: 0px;
      bottom: 0px;
      right: 0px;
      overflow: hidden;
      background-size: cover;
      background-repeat: no-repeat;
      background-position: 50% 50%;
      background-image: none;
    }
    
    #bgvid {
      margin: auto;
      position: absolute;
      z-index: -1;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      visibility: visible;
      width: 1267px;
      height: auto;
    }
    
    0 讨论(0)
提交回复
热议问题