即使 z-index 更高,下拉菜单也会出现在标题后面

Dropdown appearing behind title even with higher z-index

我正在尝试制作下拉菜单,但它出现在网站标题后面。我试图在多个地方放置 z-index: 1000; 但它没有改变任何东西,这是我的代码

.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>

实际上 h1 元素前面(你会看得更清楚,如果你将导航 link 文本设为黄色,例如)——你只需要下拉菜单的背景,否则它是透明的,产生你看到的效果。

因此,如果您将 background-color(例如 white/#FFF)添加到 .background-menu,您将得到您想要的结果:

.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>