Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute positioned HTML5 video element with negative z-index breaks background-attachment in webkit browsers

I'm using an HTML5 video element as a background layer which works fine, however, it's causing problems with other elements on the page that have a background image with the property background-attachment: fixed. The background images become unstuck, broken up, or disappear completely. If I change the z-index of the video wrapper div to something positive the problem disappears but that defeats the purpose of using it as a background layer. This problem is only occurring in webkit browsers (Chrome and Safari).

Here's the basic HTML:

<section class="fixed-background">
    <p>...</p>
</section>

<section>
    <div class="video-background">
        <video loop autoplay muted>
            <source src="video.mp4" type="video/mp4">
            <source src="video.webm" type="video/webm">
        </video>
    </div>
    <p>...</p>
</section>

<section class="fixed-background">
    <p>...</p>
</section>

And the CSS:

.fixed-background {
    background: url('image.jpg') center center;
    background-size: cover;
    background-attachment: fixed;
}
.video-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    z-index: -1;
}
.video-background video {
    min-width: 100%;
    min-height: 100%;
}

I've created a sample JSFiddle that illustrates the problem. Works fine in Firefox, but breaks in Chrome/Safari.

like image 532
WhiskerSandwich Avatar asked Oct 08 '13 21:10

WhiskerSandwich


2 Answers

Did you find a fix for this? I have the same issue, however only in safari.

Edit This post here fixed it for me.

Chrome position:fixed inside position:absolute breaking with iframe / video

Add this to all position: fixed; elements

-webkit-backface-visibility: hidden;
-webkit-transform: translateZ(0);
like image 198
Corey Harrison Avatar answered Nov 16 '22 00:11

Corey Harrison


Just wrap HTML5 video in dom element with styling rules position: relative; and overflow:hidden; This will fix everything in all browsers!

like image 43
Andris Avatar answered Nov 16 '22 02:11

Andris