CSS响应菜单,潜艇不工作

时间:2017-04-28 09:32:58

标签: html css

尝试使用子菜单创建响应式导航,第一个子菜单运行良好,但似乎无法使子子菜单起作用。

如果我可以在每个部分下添加UL's并且它会弹出,那就太棒了。

我做错了什么

.nav ul {
  list-style: none;
  background-color: #444;
  text-align: center;
  padding: 0;
  margin: 0;
}

.nav li {
  font-family: 'Oswald', sans-serif;
  font-size: 1.2em;
  line-height: 40px;
  text-align: left;
}

.nav a {
  text-decoration: none;
  color: #fff;
  display: block;
  padding-left: 15px;
  border-bottom: 1px solid #888;
  transition: .3s background-color;
}

.nav a:hover {
  background-color: #005f5f;
}

.nav a.active {
  background-color: #aaa;
  color: #444;
  cursor: default;
}


/* Sub Menus */

.nav li li {
  font-size: .8em;
}

@media screen and (min-width: 650px) {
  .nav li {
    width: 130px;
    border-bottom: none;
    height: 50px;
    line-height: 50px;
    font-size: 1.4em;
    display: inline-block;
    margin-right: -4px;
  }
  .nav a {
    border-bottom: none;
  }
  .nav>ul>li {
    text-align: center;
  }
  .nav>ul>li>a {
    padding-left: 0;
  }
  /* Sub Menus */
  .nav li ul {
    position: absolute;
    display: none;
    width: inherit;
  }
  .nav li:hover ul {
    display: block;
  }
  .nav li ul li {
    display: block;
  }
}
<header>
  <div class="nav">
    <ul class="menu">
      <li class="home"><a href="#">Home</a></li>
      <li class="tutorials"><a href="#">Tutorials</a>
        <ul>
          <li><a href="#">Tutorial #1</a></li>
          <li>
            <a href="#">Tutorial #2</a>
            <ul>
              <li><a href="#">Tutorial #1</a></li>
              <li><a href="#">Tutorial #2</a></li>
              <li><a href="#">Tutorial #3</a></li>
            </ul>
          </li>
          <li><a href="#">Tutorial #3</a></li>
        </ul>
      </li>
      <li class="about"><a class="active" href="#">About</a></li>
      <li class="news"><a href="#">Newsletter</a>
        <ul>
          <li><a href="#">News #1</a></li>
          <li><a href="#">News #2</a></li>
          <li><a href="#">News #3</a></li>
        </ul>
      </li>
      <li class="contact"><a href="#">Contact</a></li>
    </ul>
  </div>
</header>

2 个答案:

答案 0 :(得分:0)

您的菜单重叠,这就是您无法正确看到它们的原因。您可以修复此操作将子菜单向右移动并在父菜单项时显示它们,例如:

<强> HTML:

<header>
  <div class="nav">
    <ul class="menu">
      <li class="home"><a href="#">Home</a></li>
      <li class="tutorials"><a href="#">Tutorials</a>
        <ul>
          <li><a href="#">Tutorial #1</a></li>
          <li>
            <a href="#">Tutorial #2</a>
              <ul class="submenu">
                <li><a href="#">Tutorial #1</a></li>
                <li><a href="#">Tutorial #2</a></li>
                <li><a href="#">Tutorial #3</a></li>
              </ul>
          </li>
          <li><a href="#">Tutorial #3</a></li>
        </ul>
      </li>
      <li class="about"><a class="active" href="#">About</a></li>
      <li class="news"><a href="#">Newsletter</a>
        <ul>
          <li><a href="#">News #1</a></li>
          <li><a href="#">News #2</a></li>
          <li><a href="#">News #3</a></li>
        </ul>
      </li>
      <li class="contact"><a href="#">Contact</a></li>
    </ul>
  </div>
</header>

<强> CSS:

.nav ul {
  list-style: none;
  background-color: #444;
  text-align: center;
  padding: 0;
  margin: 0;
}

.nav li {
  font-family: 'Oswald', sans-serif;
  font-size: 1.2em;
  line-height: 40px;
  text-align: left;
}

.nav a {
  text-decoration: none;
  color: #fff;
  display: block;
  padding-left: 15px;
  border-bottom: 1px solid #888;
  transition: .3s background-color;
}

.nav a:hover {
  background-color: #005f5f;
}

.nav a.active {
  background-color: #aaa;
  color: #444;
  cursor: default;
}

/* Sub Menus */
.nav li li {
  font-size: .8em;
}

ul.submenu {
  margin-left:100%;
  margin-top: -50px;
}
/*******************************************
   Style menu for larger screens

   Using 650px (130px each * 5 items), but ems
   or other values could be used depending on other factors
********************************************/

@media screen and (min-width: 650px) {
  .nav li {
    width: 130px;
    border-bottom: none;
    height: 50px;
    line-height: 50px;
    font-size: 1.4em;
    display: inline-block;
    margin-right: -4px;
  }

  .nav a {
    border-bottom: none;
  }

  .nav > ul > li {
    text-align: center;
  }

  .nav > ul > li > a {
    padding-left: 0;
  }

  /* Sub Menus */
  .nav li ul {
    position: absolute;
    display: none;
    width: inherit;
  }

  .nav li:hover ul:not(.submenu) {
    display: block;
  }

  .nav li ul li {
    display: block;
  }

  .nav ul.submenu { display:none }
  .nav li:hover ul li:hover ul.submenu { display:block; }
}

答案 1 :(得分:0)

更新fddle https://jsfiddle.net/99Lecy15/2/

.nav li ul li {
  display: block;
position:relative;
}
 .nav li  ul  a +  ul{
position:absolute;
left:100%;
top:0;    
}