I've a basic parallax layout which fundamentally seems to be working, but I'm struggling with the z-index of the static sections, which has caused them to scroll behind instead of in front of certain divs
HTML:
<header class="header">
</header>
<main class="parallax-container">
<section class="parallax-section parallax-section--hero">
HERO
</section>
<section class="static-section static-section--about">
ABOUT
</section>
<section class="parallax-section parallax-section--testimonials">
TESTIMONIALS
</section>
<section class="static-section static-section--footer">
FOOTER
</section>
</main>
CSS:
* {
margin: 0;
box-sizing: inherit;
}
body {
box-sizing: border-box;
font-size: 2rem;
}
.parallax-container {
border: 1px solid red;
position: relative;
// viewport of 100vh
height: 100vh;
overflow-x: hidden;
overflow-y: auto;
// Set the perspective / simulate distance of children from the viewport
perspective: 2px;
}
section {
display: flex;
justify-content: center;
align-items: center;
}
.parallax-section, .static-section {
height: 100vh;
}
.parallax-section {
// Allow the parallax section to be positioned in the 3d space
transform-style: preserve-3d;
z-index: -1;
&::after {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border: 3px solid blue;
background-image: url(https://asiamountains.net/upload/slide/slide-1960x857-07.jpg);
background-repeat: no-repeat;
// Move the pseudo element away from the camera and scale it up to fill the viewport
transform: translateZ(-1px) scale(1.5);
// Keep the images from overlapping siblings
z-index: -1;
}
}
.static-section {
background-color: darkgrey;
color: white;
z-index: 100;
}
I've a codepen of the above problem set up here, you'll see the darkgrey static sections scroll behind the parallax background images. I've had this up and running correctly before, but I've just obviously made a mistake I can't spot. Z-index and stacking contexts always trip me up!
If anyone has the time to have a quick look it would be appreciated!