Search code examples
htmlcsshtml5-video

Object-fit: cover - is not working with video tags without border-radius


I am facing a problem, that I cannot make object-fit: cover; work with video tag because it is overlays outside the parent container, even if I use object-fit: cover;

Only one solution works: add a border-radius with some value, like:

border-radius: 10px; to the video tag css.

Working example: https://codepen.io/anon/pen/qmrvLB Comment in or out border-radius: 10px; line to see what is my problem.


Solution

  • If using border-radius: 1px; (and no one will see that radius) feels too hacky, use a wrapper

    body {
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .wrapper {
      width: 50%;
      height: 50px;
      overflow: hidden;
    }
    
    video {
      object-fit: cover;
      width: 100%;
      height: 100%;
    }
    <div class="wrapper">
      <video poster="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/polina.jpg" id="bgvid" playsinline autoplay muted loop>
    <source src="http://thenewcode.com/assets/videos/polina.webm" type="video/webm">
    <source src="http://thenewcode.com/assets/videos/polina.mp4" type="video/mp4">
      </video>
    </div>