13

I'm working on a simple example that utilizes the CSS object-fit: cover property on a video element that is wrapped inside a div with specified width and aspect ratio.

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>CSS Test</title>
  </head>
  <body>
    <div id="container">
      <video
        autoplay
        playsinline
        muted
        loop
        src="https://media.istockphoto.com/videos/slow-motion-rock-on-the-midzor-peak-video-id1248422582"
      ></video>
    </div>
  </body>
</html>

CSS

* {
  box-sizing: border-box;
}
html,
body {
  margin: 0;
  padding: 1rem;
  width: 100%;
  height: 100%;
}
#container {
  width: 500px;
  overflow: hidden;
  aspect-ratio: 1/1;
  border: 1px solid red;
}
video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

I noticed that upon loading the page in Safari, the video initially displays at a smaller scale within the div, before jumping to its intended scale and position. I attached a GIF below to show this behavior:

object: fit bug safari

The same code works perfectly well in Chrome and Firefox. I tried specifying the object-position property without success.

Any ideas as to what might be causing this behavior in Safari specifically?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Zampus
  • 143
  • 5

2 Answers2

1

Specifying the height (either on the <video> element or in css) seems to solve the problem:

  <body>
    <div id="container">
      <video
        height="500px"
        autoplay
        playsinline
        muted
        loop
        src="https://media.istockphoto.com/videos/slow-motion-rock-on-the-midzor-peak-video-id1248422582"
      ></video>
    </div>
  </body>
  • A fixed video height isn't ideal, but it's a solution. My hunch is the browser's rendering engine changed recently. I didn't run into this problem before. A fixed height means the browser knows to reserve 500px of space on initial render. – dwu39 Apr 18 '23 at 23:47
-2

To fix min temporarily, I used framer motion to delay the showing of video like this

<AnimationFadeIn initial={{ opacity: 0 }} animate={{ opacity: 1 }} transition={{ duration: 0.3, delay: 0.4 }}>
                <video width="100%" height="100%" autoPlay loop muted playsInline>
                    <source src="/animation-video-3.mov" type="video/mp4; codecs=hvc1" />
                    <source src="/animation-video-3-vp9-chrome.webm" type="video/webm" />
                </video>
</AnimationFadeIn>

You see a delay of 0.4 in the transition property!

But I still don't know how to fix it without delay.

Nazariy
  • 717
  • 6
  • 23