【问题标题】:HTML Nav bar links not clickableHTML 导航栏链接不可点击
【发布时间】:2019-06-22 08:18:26
【问题描述】:

上周我一直在学习 HTML 和 CSS,并决定使用导航栏。我已经设置好了,但现在顶部的链接不可点击? 我已经检查以确保我没有错过任何基本的东西,比如结束标签,但找不到类似的东西。

* {
  margin: 0;
  padding: 0;
}

html {
  background-color: #808080;
  background-image: url("https://hdqwalls.com/download/simple-gray-abstract-background-wi-1920x1080.jpg");
  background-repeat: no-repeat;
}

#landing__container {
  height: 100vh;
  width: 100%;
}

header {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(255, 0, 0, 0.4);
}

header h1 {
  color: white;
  font-family: impact;
  font-weight: normal;
}

header ul {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 30px;
}

header ul li {
  list-style: none;
}

header a {
  font-family: helvetica;
  font-weight: bold;
  font-size: 18px;
  text-decoration: none;
  color: white;
  margin: 30px;
}

#landing__center {
  display: flex;
  flex-direction: column;
  text-align: center;
  justify-content: center;
  align-items: center;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  position: absolute;
}

#landing__center h2 {
  padding-bottom: 20px;
  color: white;
  font-family: Impact;
  font-weight: normal;
  font-size: 40px;
}

#landing__center p {
  padding-top: 20px;
  color: white;
  font-family: helvetica;
  font-weight: normal;
  font-size: 20px;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Navigation Bar Practice</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>

  <body>
    <div id="landing__container">
      <!-- Navigation Bar -->
      <header>
        <nav id="nav__bar">
          <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Documentation</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
        </nav>
      </header>

      <section id="landing__center">
        <h2>Testing</h2>
        <p>
          Lorem, ipsum dolor sit amet consectetur adipisicing elit. Rem
          voluptatum asperiores illum maiores aliquid quo perferendis,
          blanditiis facere temporibus laudantium!
        </p>
      </section>
    </div>
  </body>
</html>

我知道代码可能并不完美和/或干净,但请记住,我已经这样做了一周。

【问题讨论】:

  • z-index:999; 添加到您的header ul li

标签: css html nav


【解决方案1】:

您已经制作了此部分 #landing__center position:absolutetop:0 这就是它出现在导航栏顶部的原因。我已将顶部增加到 30。谢谢

* {
  margin: 0;
  padding: 0;
}

html {
  background-color: #808080;
  background-image: url("https://hdqwalls.com/download/simple-gray-abstract-background-wi-1920x1080.jpg");
  background-repeat: no-repeat;
}

#landing__container {
  height: 100vh;
  width: 100%;
}

header {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(255, 0, 0, 0.4);
}

header h1 {
  color: white;
  font-family: impact;
  font-weight: normal;
}

header ul {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 30px;
}

header ul li {
  list-style: none;
}

header a {
  font-family: helvetica;
  font-weight: bold;
  font-size: 18px;
  text-decoration: none;
  color: white;
  margin: 30px;
}

#landing__center {
  display: flex;
  flex-direction: column;
  text-align: center;
  justify-content: center;
  align-items: center;
  top: 30; /*changed*/
  bottom: 0;
  left: 0;
  right: 0;
  position: absolute;
}

#landing__center h2 {
  padding-bottom: 20px;
  color: white;
  font-family: Impact;
  font-weight: normal;
  font-size: 40px;
}

#landing__center p {
  padding-top: 20px;
  color: white;
  font-family: helvetica;
  font-weight: normal;
  font-size: 20px;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Navigation Bar Practice</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>

  <body>
    <div id="landing__container">
      <!-- Navigation Bar -->
      <header>
        <nav id="nav__bar">
          <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Documentation</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
        </nav>
      </header>

      <section id="landing__center">
        <h2>Testing</h2>
        <p>
          Lorem, ipsum dolor sit amet consectetur adipisicing elit. Rem
          voluptatum asperiores illum maiores aliquid quo perferendis,
          blanditiis facere temporibus laudantium!
        </p>
      </section>
    </div>
  </body>
</html>

【讨论】:

  • 看全页,header被推到了中心下方
  • 你能解释一下你想说什么吗?谢谢
  • 运行sn-p,然后点击整页
【解决方案2】:

您需要将z-indexposition: relative 添加到您的代码中

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

【讨论】:

    【解决方案3】:

    问题是由#landing__center覆盖导航栏引起的,因为它选择了#landing__container作为与之相关的元素

    一种方法是创建一个包装器来充当#landing__center 的容器,而不是 #landing__container。这样它就相对于它自己的容器而不是它的父容器,不干扰它的兄弟姐妹

    * {
      margin: 0;
      padding: 0;
    }
    
    html {
      background-color: #808080;
      background-image: url("https://hdqwalls.com/download/simple-gray-abstract-background-wi-1920x1080.jpg");
      background-repeat: no-repeat;
    }
    
    #landing__container {
      height: 100vh;
      width: 100%;
    }
    
    header {
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: rgba(255, 0, 0, 0.4);
    }
    
    header h1 {
      color: white;
      font-family: impact;
      font-weight: normal;
    }
    
    header ul {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      height: 30px;
    }
    
    header ul li {
      list-style: none;
    }
    
    header a {
      font-family: helvetica;
      font-weight: bold;
      font-size: 18px;
      text-decoration: none;
      color: white;
      margin: 30px;
    }
    
    .wrapper {
      position: relative;
      height: 95%;
    }
    
    #landing__center {
      display: flex;
      flex-direction: column;
      text-align: center;
      justify-content: center;
      align-items: center;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      position: absolute;
    }
    
    #landing__center h2 {
      padding-bottom: 20px;
      color: white;
      font-family: Impact;
      font-weight: normal;
      font-size: 40px;
    }
    
    #landing__center p {
      padding-top: 20px;
      color: white;
      font-family: helvetica;
      font-weight: normal;
      font-size: 20px;
    }
    <body>
      <div id="landing__container">
        <!-- Navigation Bar -->
        <header>
          <nav id="nav__bar">
            <ul>
              <li><a href="#">Home</a></li>
              <li><a href="#">Documentation</a></li>
              <li><a href="#">About</a></li>
              <li><a href="#">Contact</a></li>
            </ul>
          </nav>
        </header>
    
        <div class="wrapper">
          <section id="landing__center">
            <h2>Testing</h2>
            <p>
              Lorem, ipsum dolor sit amet consectetur adipisicing elit. Rem voluptatum asperiores illum maiores aliquid quo perferendis, blanditiis facere temporibus laudantium!
            </p>
          </section>
        </div>
      </div>
    </body>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多