Html 和 Css 导航栏问题

Html and Css issue with navigation bar

我是 html 和 css 的新手,但我的导航栏和汉堡菜单有问题,li a 元素在我的图像后面,我似乎无法修复这个问题。如果您还可以查看第一张图片,您会看到 li a 元素和深红色背景,这意味着不在顶部。

nav {
  height: 80px;
  background: crimson;
  opacity: 80%;
}
nav h1 {
  width: 400px;
  position: absolute;
  top: 20px;
  left: 12%;
  color: white;
  font-size: 30px;
}
nav ul {
  float: right;
  margin-right: 25px;
}
nav ul li {
  display: inline-block;
  line-height: 80px;
  margin: 0 15px;
}
nav ul li a {
  position: relative;
  color: white;
  font-size: 18px;
  padding: 5px 0;
  text-transform: uppercase;
}
nav ul li a:before {
  position: absolute;
  content: "";
  left: 0;
  bottom: 0;
  height: 3px;
  width: 100%;
  background: white;
  transform: scaleX(0);
  transform-origin: right;
  transition: transform 0.4s linear;
}
nav ul li a:hover:before {
  transform: scaleX(1);
  transform-origin: left;
}
label #btn,
label #cancel {
  color: white;
  font-size: 30px;
  float: right;
  line-height: 80px;
  margin-right: 40px;
  cursor: pointer;
  display: none;
}
#check {
  display: none;
}
@media (max-width: 1118px) {
  nav h1 {
    left: 8%;
  }
}
@media (max-width: 944px) {
  nav h1 {
    left: 6%;
    top: 20px;
    width: 300px;
  }
  nav ul li a {
    font-size: 17px;
  }
}
@media (max-width: 860px) {
  label #btn {
    display: block;
  }
  ul {
    position: fixed;
    width: 100%;
    height: 100vh;
    background: crimson;
    top: 80px;
    left: 0;
    text-align: center;
  }
  nav ul li {
    display: block;
    margin: 50px;
    line-height: 30px;
  }
  nav ul li a {
    font-size: 20px;
  }
}
<nav>
    <input type="checkbox" id="check">
    <label for="check">
        <i class="fas fa-bars" id="btn"></i>
        <i class="fas fa-times" id="cancel"></i>
    </label>
    <h1>Benji's Equestrian</h1>
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Store</a></li>
        <li><a href="#">FAQ</a></li>
        <li><a href="#">Contact Us</a></li>
    </ul>
</nav>

上面是导航栏的当前 css,下面是 post html。

<nav>
<input type="checkbox" id="check">
<label for="check">
    <i class="fas fa-bars" id="btn"></i>
    <i class="fas fa-times" id="cancel"></i>
</label>
<h1>Benji's Equestrian</h1>
<ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Store</a></li>
    <li><a href="#">FAQ</a></li>
    <li><a href="#">Contact Us</a></li>
</ul>

导航栏和元素的问题:

导航栏

@media (max-width: 860px) {
  label #btn {
    display: block;
  }
  ul {
    position: fixed;
    width: 100%;
    height: 100vh;
    background: crimson;
    top: 80px;
    left: -100%;
    text-align: center;
    transition: all 0.5s;
  }
  nav ul li {
    display: block;
    margin: 50px;
    line-height: 30px;
  }
  nav ul li a {
    font-size: 20px;
  }
  #check:checked ~ ul {
    left: 0;
  }
}

上面的css是我认为的问题所在,我试过添加z-index:100;到 .nav 元素,它没有解决问题。

如果菜单元素必须位于图像的顶部,则必须使用 z-index。只需添加 z-index 并将其声明为 100 或 1000。不要向元素添加任何 position(相对、绝对等)值。

nav ul li {
    display: inline-block;
    line-height: 80px;
    margin: 0 15px;
    position:relative;
    z-index:100;
}

如果你想确保你的 nav 最重要,那么只需将 z-index 添加到大于 0 的任何数量,或者像人们通常做的那样 100 甚至 1000:-

nav {
  height: 80px;
  background: crimson;
  opacity: 80%;
  z-index: 100
}