Html5 video background, keep center of video in center

后端 未结 10 1454
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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

    Catchy title

    Everyone loves catchy titles

    New paragaph

    Some random text goes here...

提交回复
热议问题