Search code examples
htmlcsscss-positionz-index

Dropdown appearing behind title even with higher z-index


i'm trying to make a dropdown, but it appears behind the website title. I tried to put z-index: 1000; at multiple places but it didn't change anything, here is my code

enter image description here

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 110px;
  font-weight: 600;
  font-size: 15px;
  line-height: 18px;
}

.header-nav>ul {
  display: flex;
  align-items: center;
  padding-left: 0;
  gap: 69px;
}

.header-nav>ul li {
  list-style: none;
}

.header-nav>ul li:nth-child(2) {
  color: var(--info-text-color);
}

.header-link {
  padding: 5px 10px;
}

.header-right {
  box-shadow: -5px 5px 34px rgba(0, 0, 0, 0.13);
  border-radius: 34px;
  padding: 13px 34px;
}

#logo {
  font-size: 21px;
  line-height: 26px;
  font-family: Righteous, cursive;
  font-weight: 500;
  background: linear-gradient(90deg, #000000 0%, #9e9e9e 48.44%, #dadada 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  padding-left: 0;
}

.dropdown {
  z-index: 1000;
  position: relative;
}

.dropdown-title {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 13px;
}

.dropdown-menu {
  display: grid;
  z-index: 1000;
  box-shadow: -5px 5px 34px rgba(0, 0, 0, 0.13);
  border-radius: 8px;
  position: absolute;
  transform: scale(0);
  transform-origin: top;
  transition: all 0.3s ease-in-out;
  place-items: center;
  padding: 20px;
}

.dropdown-menu ul {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-left: 0;
}

.dropdown-menu ul li {
  width: max-content;
}

.dropdown:hover .dropdown-menu {
  transform: scale(1);
}

.dropdown:hover img {
  transform: rotate(180deg);
}

.dropdown img {
  transition: all 0.3s ease-in-out;
}
<header class="header">
  <nav class="header-nav">
    <ul>
      <li>
        <a class="header-link" href="#"> Getting Started </a>
      </li>
      <li class="dropdown">
        <span class="header-link dropdown-title">
            Resources
            <img src="@/assets/svgs/dropdown.svg" alt="" />
        </span>
        <div class="dropdown-menu">
          <ul>
            <li>
              <a href="#"> API Reference </a>
            </li>
            <li>
              <a href="#"> Privacy Policy </a>
            </li>
            <li>
              <a href="#"> Terms Conditions</a>
            </li>
          </ul>
        </div>
      </li>
    </ul>
  </nav>
  <div class="header-right">continue login</div>
</header>
<div class="hero">
  <div class="hero-left">
    <h1>create open digital commerce networks</h1>
  </div>
</div>


Solution

  • Actually the dropdown is in front of the h1 element (you'd see more clearly that if you made the nav link texts yellow, for example) – you just need a background for the dropdown menu, because otherwise it's transparent, producing the effect you saw.

    So, if you add a background-color (for example white/#FFF) to .background-menu, you'll get the result you want:

    .header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 110px;
      font-weight: 600;
      font-size: 15px;
      line-height: 18px;
    }
    
    .header-nav>ul {
      display: flex;
      align-items: center;
      padding-left: 0;
      gap: 69px;
    }
    
    .header-nav>ul li {
      list-style: none;
    }
    
    .header-nav>ul li:nth-child(2) {
      color: var(--info-text-color);
    }
    
    .header-link {
      padding: 5px 10px;
    }
    
    .header-right {
      box-shadow: -5px 5px 34px rgba(0, 0, 0, 0.13);
      border-radius: 34px;
      padding: 13px 34px;
    }
    
    #logo {
      font-size: 21px;
      line-height: 26px;
      font-family: Righteous, cursive;
      font-weight: 500;
      background: linear-gradient(90deg, #000000 0%, #9e9e9e 48.44%, #dadada 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      padding-left: 0;
    }
    
    .dropdown {
      z-index: 1000;
      position: relative;
    }
    
    .dropdown-title {
      cursor: pointer;
      display: flex;
      align-items: center;
      gap: 13px;
    }
    
    .dropdown-menu {
      display: grid;
      z-index: 1000;
      box-shadow: -5px 5px 34px rgba(0, 0, 0, 0.13);
      border-radius: 8px;
      position: absolute;
      transform: scale(0);
      transform-origin: top;
      transition: all 0.3s ease-in-out;
      place-items: center;
      padding: 20px;
      background: #fff;
    }
    
    .dropdown-menu ul {
      display: flex;
      flex-direction: column;
      gap: 10px;
      padding-left: 0;
    }
    
    .dropdown-menu ul li {
      width: max-content;
    }
    
    .dropdown:hover .dropdown-menu {
      transform: scale(1);
    }
    
    .dropdown:hover img {
      transform: rotate(180deg);
    }
    
    .dropdown img {
      transition: all 0.3s ease-in-out;
    }
    <header class="header">
      <nav class="header-nav">
        <ul>
          <li>
            <a class="header-link" href="#"> Getting Started </a>
          </li>
          <li class="dropdown">
            <span class="header-link dropdown-title">
                Resources
                <img src="@/assets/svgs/dropdown.svg" alt="" />
            </span>
            <div class="dropdown-menu">
              <ul>
                <li>
                  <a href="#"> API Reference </a>
                </li>
                <li>
                  <a href="#"> Privacy Policy </a>
                </li>
                <li>
                  <a href="#"> Terms Conditions</a>
                </li>
              </ul>
            </div>
          </li>
        </ul>
      </nav>
      <div class="header-right">continue login</div>
    </header>
    <div class="hero">
      <div class="hero-left">
        <h1>create open digital commerce networks</h1>
      </div>
    </div>