【问题标题】:Links are not clickable due to CSS?由于CSS,链接不可点击?
【发布时间】:2019-09-22 14:52:57
【问题描述】:

我遇到了一个问题,即我的导航栏中的链接不可点击,除非我删除了一些已添加的 CSS。

这是我需要删除的 CSS 的 sn-p 以获得可点击的链接:

.main-nav {
  float: right;
  list-style: none;
  margin-top: 30px;
}

它似乎与float: right; 相关,因为如果我只禁用该短语,链接就会变得可点击。

{
  margin: 0;
  padding: 0;
}

header {
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(zomb2.PNG);
  height: 100vh;
  background-size: cover;
  background-position: center;
}

.main-nav {
  float: right;
  list-style: none;
  margin-top: 30px;
}

.main-nav li {
  display: inline-block;
}

.main-nav li a {
  color: white;
  text-decoration: none;
  padding: 5px 20px;
  font-family: "Roboto", sans-serif;
  font-weight: bold;
}

.main-nav li.active {
  border: 1px solid white;
}

.main-nav li a:hover {
  border: 1px solid white;
}

.logo {
  width: 150px;
  height: auto;
  float: left;
  color: white;
  font-size: 20px;
}

body {
  font-family: monospace;
}

.row {
  max-width: 1200px;
  margin: auto;
}

.hero {
  position: absolute;
  width: 1200px;
  margin-left: 0px;
  margin-top: 0px;
}

h1 {
  color: white;
  text-transform: uppercase;
  font-size: 70px;
  text-align: center;
  margin-top: 275px;
}

h2 {
  color: white;
  text-transform: uppercase;
  font-size: 35px;
  text-align: center;
}

.button {
  margin-top: 30px;
  margin-left: 440px;
}

.btn {
  border: 1px solid white;
  padding: 10px 30px;
  color: white;
  text-decoration: none;
  margin-right: 5px;
  font-size: 13px;
  text-transform: uppercase;
}

.btn-one {
  background-color: darkorange;
  font-family: "Roboto", sans-serif;
}

.btn-two {
  background-color: darkorange;
  font-family: "Roboto", sans-serif;
}

.btn-two: hover {
  background-color: darkorange;
}
<header>
  <div class="row">
    <div class="logo">
    </div>

    <ul class="main-nav">
      <li class="active"><a href="">Home</a></li>
      <li><a href="about.html">Learn More</a></li>
      <li><a href="zombies.html">Real Zombies</a></li>
      <li><a href="ordinance.html">Guns and Ammo</a></li>
      <li><a href="news.html">The Latest</a></li>
      <li><a href="faq.html">FAQ</a></li>
    </ul>
  </div>

  <div class="hero">
    <h1> Real Zombie Vs. Human Battles </h1>
    <h2> Choose your side..</h2>
    <div class="button">
      <a href="" class="btn btn-one">Zombies</a>
      <a href="" class="btn btn-two">Humans</a>
    </div>
  </div>

提前感谢您的帮助!我确定我错过了一些简单的东西,但我在任何地方都找不到它。我在这里四处搜索,找不到任何专门解决这个问题的东西。救命!

【问题讨论】:

  • 尝试使用 z-index。希望有帮助
  • .hero { z-index: -1; }

标签: html css


【解决方案1】:

.hero 在position:absolute 中与header 重叠,您可以通过position:/* any value but static */ + z-index 将您的链接放在上面

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

演示:

{
  margin: 0;
  padding: 0;
}

header {
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(zomb2.PNG);
  height: 100vh;
  background-size: cover;
  background-position: center;
}

.main-nav {
  float: right;
  list-style: none;
  margin-top: 30px;
}

.main-nav li {
  display: inline-block;
}

.main-nav li a {
  color: white;
  text-decoration: none;
  padding: 5px 20px;
  font-family: "Roboto", sans-serif;
  font-weight: bold;
  position:relative;
  z-index:1;
}

.main-nav li.active {
  border: 1px solid white;
}

.main-nav li a:hover {
  border: 1px solid white;
}

.logo {
  width: 150px;
  height: auto;
  float: left;
  color: white;
  font-size: 20px;
}

body {
  font-family: monospace;
}

.row {
  max-width: 1200px;
  margin: auto;
}

.hero {
  position: absolute;
  width: 1200px;
  margin-left: 0px;
  margin-top: 0px;
}

h1 {
  color: white;
  text-transform: uppercase;
  font-size: 70px;
  text-align: center;
  margin-top: 275px;
}

h2 {
  color: white;
  text-transform: uppercase;
  font-size: 35px;
  text-align: center;
}

.button {
  margin-top: 30px;
  margin-left: 440px;
}

.btn {
  border: 1px solid white;
  padding: 10px 30px;
  color: white;
  text-decoration: none;
  margin-right: 5px;
  font-size: 13px;
  text-transform: uppercase;
}

.btn-one {
  background-color: darkorange;
  font-family: "Roboto", sans-serif;
}

.btn-two {
  background-color: darkorange;
  font-family: "Roboto", sans-serif;
}

.btn-two: hover {
  background-color: darkorange;
}
<header>
  <div class="row">
    <div class="logo">
    </div>

    <ul class="main-nav">
      <li class="active"><a href="">Home</a></li>
      <li><a href="about.html">Learn More</a></li>
      <li><a href="zombies.html">Real Zombies</a></li>
      <li><a href="ordinance.html">Guns and Ammo</a></li>
      <li><a href="news.html">The Latest</a></li>
      <li><a href="faq.html">FAQ</a></li>
    </ul>
  </div>

  <div class="hero">
    <h1> Real Zombie Vs. Human Battles </h1>
    <h2> Choose your side..</h2>
    <div class="button">
      <a href="" class="btn btn-one">Zombies</a>
      <a href="" class="btn btn-two">Humans</a>
    </div>
  </div>

【讨论】:

  • 谢谢!我也试过你的,看起来 z-index 解决了这个问题。
【解决方案2】:

尝试将您的 css 更改为

.main-nav
{
     float: right;
     list-style: none;
     margin-top: 30px;
     position: relative;
     z-index: 10;
}
z-index 应该可以解决这个问题。 (但如果没有将位置设置为相对,它将无法工作)

欲了解更多信息,请查看此链接:https://developer.mozilla.org/en-US/docs/Web/CSS/z-index

【讨论】:

    猜你喜欢
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 2012-12-26
    • 1970-01-01
    • 2021-04-02
    • 2021-09-13
    • 1970-01-01
    相关资源
    最近更新 更多