HenryM HenryM - 2 months ago 13
CSS Question

Getting background image to scroll

I'm running a background video when visitors enter the landing page. As you scroll down the page the video stays in place but I would like to change this so the video scrolls out as well. I'm not sure how to do that.

Here is my CSS:

video#bgvid {
position: fixed;
top: 50%;
left: 50%;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
z-index: -100;
-ms-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-webkit-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
background: url() no-repeat;
background-size: cover;
}

.video-container {
min-height: 100vh;
}
.video-container-bg {
padding-top: 45vh;
color: #fff;
}


And here is the HTML which uses it:

<div class="video-container">
<div class="video-container-bg">
<video playsinline autoplay muted loop poster="{{page.image.url}}" id="bgvid">
<source src="{{page.video.url}}" type="video/mp4">
</video>
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-8">
<div class="animation-element bounce-up">
<h1 class="page-title">{{page.page_title}}</h1>
<p class="strapeline">{{page.strapline}}</p>
<a class="butt" href="#about-us">Learn More</a>
</div>
</div>
</div>
</div>
</div>
</div>

Answer

Make position:relative instead of fixed

video#bgvid { 
    position: relative;
    top: 50%;
    left: 50%;
    min-width: 100%;
    min-height: 100%;
    width: auto;
    height: auto;
    z-index: -100;
    -ms-transform: translateX(-50%) translateY(-50%);
    -moz-transform: translateX(-50%) translateY(-50%);
    -webkit-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);
    background: url() no-repeat;
    background-size: cover; 
}

.video-container {
    min-height: 100vh;
}
.video-container-bg {
    padding-top: 45vh;
    color: #fff;
}
<div class="video-container">
    <div class="video-container-bg">
        <video playsinline autoplay muted loop poster="{{page.image.url}}" id="bgvid">
                <source src="{{page.video.url}}" type="video/mp4">
        </video>
        <div class="container">
            <div class="row">
                <div class="col-sm-12 col-md-8">
                    <div class="animation-element bounce-up">
                        <h1 class="page-title">{{page.page_title}}</h1>
                        <p class="strapeline">{{page.strapline}}</p>
                        <a class="butt" href="#about-us">Learn More</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="video-container">
    <div class="video-container-bg">
        <video playsinline autoplay muted loop poster="{{page.image.url}}" id="bgvid">
                <source src="{{page.video.url}}" type="video/mp4">
        </video>
        <div class="container">
            <div class="row">
                <div class="col-sm-12 col-md-8">
                    <div class="animation-element bounce-up">
                        <h1 class="page-title">{{page.page_title}}</h1>
                        <p class="strapeline">{{page.strapline}}</p>
                        <a class="butt" href="#about-us">Learn More</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="video-container">
    <div class="video-container-bg">
        <video playsinline autoplay muted loop poster="{{page.image.url}}" id="bgvid">
                <source src="{{page.video.url}}" type="video/mp4">
        </video>
        <div class="container">
            <div class="row">
                <div class="col-sm-12 col-md-8">
                    <div class="animation-element bounce-up">
                        <h1 class="page-title">{{page.page_title}}</h1>
                        <p class="strapeline">{{page.strapline}}</p>
                        <a class="butt" href="#about-us">Learn More</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Comments