Search code examples

z-index not working with elements linked to gsap animation

I am not able to show the fixed button when scrolling over images, I tried different things but nothing seems to work even if I z-index:999999999999999;.

Image uses smooth scrolling with gsap, now sure what should be change or if z-index property doesn't work with transform: translate(-50%, -50%)..... property which changes for image wrapper when scrolling.

const select = e => document.querySelector(e);
const selectAll = e => document.querySelectorAll(e);

const panels = selectAll(".panel")

function startAnim() {
  panels.forEach((panel, i) => {
    let imageWrappers = panel.querySelectorAll(".col__image-wrap")
    gsap.fromTo(imageWrappers, {
      y: "-30vh"
    }, {
      y: "30vh",
      scrollTrigger: {
        trigger: panel,
        scrub: true,
        start: "top bottom", // position of trigger meets the scroller position
      ease: 'none'

function init() {

window.onload = () => init()

const lenis = new Lenis()

lenis.on('scroll', (e) => {

function raf(time) {

.enquire-btn{ background-color: #222; color: #fff; min-width:150px; 
  padding:10px 25px; border-radius: 100px; text-decoration: none; 
  font-weight: normal; font-size: 14px;
  position: -webkit-sticky;
  position: sticky;
.header{hight:60px; min-height:60px; background:#fff; ;
.h-line{height:1px; background:#000; margin-top:15px; }
.hero-section{margin-top:200px; z-index:20;}
.hs {
  text-transform: uppercase;
 letter-spacing: -5px;
font-size: clamp(2.1875rem, 0.9037rem + 5.4054vw, 9.6875rem);
.hs:first-child {font-weight:600; padding-right:10px;}

*, .panel, .project {
  padding: 0;
  margin: 0;

/* Full width section */
.panel {
  display: flex;
  justify-content: center;
  align-items: stretch;
  height: 90vh;
  overflow: hidden;
  margin-top: 0vh;
/*     background-color: red; */

/* In charge of setting the height/width/area */
.column {
  position: relative;
  overflow: hidden;

/* Extra height for the parallax effect */
.col__image-wrap {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
    height: 160vh;
    background-color: red;

/* make sure it covers the total space */
.img {
    object-fit: cover;
    width: 100%;
    height: 100%;

.intro-section{min-height:300px; background:grey; padding:30px 0px;}
.intro-section .section-heading {
  position:relative; withd:100%; padding:30px; 
  color:#fff; font-size:18px;}
<link href="" rel="stylesheet"/>
<link href="" rel="stylesheet"/>

<header class="container-fluid text-center header animate__animated animate__fadeIn animate__delay-800ms">
  <div class="row justify-content-evenly vertical-align p-3 ">
    <div class="col-4 align-middle">AHRE</div>
    <div class="col-4 ms-auto">      <a class="enquire-btn animate__animated animate__fadeInUp animate__slower">CONTACT US</a></div>
    <div class="h-line animate__animated animate__slideInLeft animate__delay-900ms"></div>
<section class="container-fluid hero-section">
  <span class="hs animate__animated animate__fadeInUp animate__delay-1s">The Residences</span>
<section class="container-fluid section2 p-0">
   <div class="panel panel--1">
    <div class="column">
      <div class="col__image-wrap">
      <img class="img img--1" src="" />
<section class="intro-section">
  <div> <span class="section-heading">Introduction</span></div>

<script src="[email protected]/dist/lenis.min.js"></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>


  • The button you are trying to raise up using z-index is in a different stacking context (more information on stacking context can be found using that link) to the section that is currently overlapping it. Because of this, no matter how high you make the z-index of the button, as you have seen, you will not be able to raise it above the image. Increasing the z-index is raising the button in relation to the header it sits in.

    What you need to do is raise the z-index of the header to sit above the section(s) that follow. In this example codepen showing the solution you can see all I have changed is I have added the following lines to your CSS and commented out the button's z-index value to show it is not needed:

    header {
        z-index: 1;
        position: relative;