如何让链接在我的导航栏中工作?

How do I get the links to work in my navigation bar?

所以我一直在修补 HTML 和 CSS 一点点(请原谅我的演示),我一直在尝试让固定的导航栏与页内链接一起使用我的一页网站中的不同部分。我今天早上把它搞砸了,但我终究无法获得工作链接。

我有一个 google 并且有一些类似的帖子说 z-index 是问题,但我已经尝试过它但我仍然无法让它们工作.

...有人愿意为我解释一下吗?

html {

}

body{
  font-family: 'Nunito';
  background: linear-gradient(#8FB0B7, #E8EFF0);
}

header {
  background-color: #e6763c;
  box-sizing: border-box;
  padding: 0 2rem;
  text-align: center;
  position: fixed;
  top: 0px;
  right: 0px;
  width: 100vw;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1;
}

ul {
  margin: 0;
  padding: 20px;
  list-style: none;
  /* border: 1px solid black; */

}

li {
  display: inline-block;
  padding: 10px;
  margin: 0 0 0 2.5em;
  background: #DDCFB199;
  text-align: center;
  border-radius: 5px;
  transition: all 0.2s ease;
}

.container {
  height: 100vh;
}
/* On hover animations */
li:hover{
  box-shadow: 0px 0px 7px 7px #3333;
  transform: scale(1.02);


}
/* Mobile */

@media (max-width: 900px) {
  header {
    background-color: #e6763c;
    box-sizing: border-box;
    padding-left: 0.2rem;
    right: 0px;
    text-align: center;
    position: fixed;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
  }

  li {
    display: block;
    margin:   1.2rem;

  }
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Nunito" rel="stylesheet">
    <link href="styleSheet.css" rel="stylesheet"></link>
    <title>Original Tombstones</title>
  </head>
  <body>
<header>
  <h1>Original Tombstones</h1>
  <ul class="navbar">
    <li><a href="#new"></a>New Memorials</li>
    <li><a href="#additionals"></a>Additionals</li>
    <li><a href="#renovations"></a>Renovations</li>
    <li><a href="#contact"></a>Contact Us</li>
  </ul>
</header>
<div class="container" id="home">
</div>
<hr>
<div class="container" id="new">
</div>
<hr>
<div class="container" id="additionals">
</div>
<hr>
<div class="container" id="renovations">
</div>
<hr>
<div class="container" id="contact">
</div>

  </body>
</html>

您的 <a> 标签内的 innerHTML 是空的。这对我有用 将此放在您的链接中:

<li><a href="#new">New Memorials</a></li>

您的文字未包含在锚标记中。您必须将 </a> 标记放在结束 </li> 标记之前,如下所示:

<header>
  <h1>Original Tombstones</h1>
  <ul class="navbar">
    <li><a href="#new">New Memorials</a></li>
    <li><a href="#additionals">Additionals</a></li>
    <li><a href="#renovations">Renovations</a></li>
    <li><a href="#contact">Contact Us</a></li>
  </ul>
</header>

尝试将文本放在 <a> 标签内,即

而不是:

<li><a href="#renovations"></a>Renovations</li>

尝试

<li><a href="#renovations">Renovations</a></li>

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  height: 100vh;
  width: 100%;
  font-family: "Nunito";
  background: linear-gradient(#8fb0b7, #e8eff0);
}

nav {
  width: 100%;
  position: fixed;
  top: 0;
  z-index: 9999px;
  background-color: #e6763c;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2rem;
  text-align: center;
}

ul {
  padding: 20px;
  list-style: none;
  /* border: 1px solid black; */
}

li {
  display: inline-block;
  padding: 10px;
  margin: 0 0 0 2.5em;
  background: #ddcfb199;
  border-radius: 5px;
  transition: all 0.2s ease;
}

.container {
  height: 100%;
}
/* On hover animations */
li:hover {
  box-shadow: 0px 0px 7px 7px #3333;
  transform: scale(1.02);
}
li a {
  text-decoration: none;
  color: #000;
}
/* Mobile */

@media (max-width: 900px) {
  nav {
    background-color: #e6763c;
    padding-left: 0.2rem;
    flex-direction: column;
    justify-content: space-around;
  }

  li {
    display: block;
    margin: 1.2rem;
  }
}
<header>
  <nav>
    <h1>Original Tombstones</h1>
    <ul class="navbar">
      <li><a href="#new">New Memorials</a></li>
      <li><a href="#additionals">Additionals</a></li>
      <li><a href="#renovations">Renovations</a></li>
      <li><a href="#contact">Contact Us</a></li>
    </ul>
  </nav>
</header>
<div class="container" id="home">
</div>
<hr>
<div class="container" id="new">
</div>
<hr>
<div class="container" id="additionals">
</div>
<hr>
<div class="container" id="renovations">
</div>
<hr>
<div class="container" id="contact">
</div>

您的链接是空的。文本位于结束锚标记 </a> 之外,因此它们充当列表项而不是链接。将文本放在锚标记内。

<ul class="navbar">
<li><a href="#new">New Memorials</a></li>
<li><a href="#additionals">Additionals</a></li>
<li><a href="#renovations">Renovations</a></li>
<li><a href="#contact">Contact Us</a></li>
</ul>