html - 导航栏列表项未显示且导航栏未固定在顶部

标签 html css flexbox navbar nav

我有导航栏,但有 2 个问题。虽然它似乎有响应,但有 2 个问题。 1. 在 968px 和大约 2001px 之间,列表项在单击或悬停时不显示。大约 2000px 后,列表项显示。问题 2。我希望导航栏在向下滚动时保持在页面顶部。我试着弄乱定位,但没有任何效果。这是我的代码

    * {
      margin: 0;
      padding: 0;
      }
    nav{
      background: #1b1b1b;
      display:flex;
      justify-content:space-around;
    }
    nav:after{
      content: '';
      clear: both;
      display: table;
    }
    nav .logo{
      
      color: white;
      font-size: 27px;
      font-weight: 600;
      line-height: 70px;
      padding-left: 60px;
    }
    nav ul{
      float: right;
      margin-right: 40px;
      list-style: none;
      position: relative;
    }
    nav ul li{
      
      display: inline-block;
      background: #1b1b1b;
      margin: 0 5px;
    }
    nav ul li a{
      color: white;
      line-height: 70px;
      text-decoration: none;
      font-size: 18px;
      padding: 8px 15px;
    }
    nav ul li a:hover{
      color: cyan;
      border-radius: 5px;
      box-shadow:  0 0 5px #33ffff,
                   0 0 10px #66ffff;
    }
    nav ul ul li a:hover{
      box-shadow: none;
    }
    nav ul ul{
      position: absolute;
      top: 90px;
      border-top: 3px solid cyan;
      opacity: 0;
      visibility: hidden;
      transition: top .3s;
    }
    nav ul ul ul{
      border-top: none;
    }
    nav ul li:hover > ul{
      top: 70px;
      opacity: 1;
      visibility: visible;
    }
    nav ul ul li{
      position: relative;
      margin: 0px;
      width: 150px;
      float: none;
      display: list-item;
      border-bottom: 1px solid rgba(0,0,0,0.3);
    }
    nav ul ul li a{
      line-height: 50px;
    }
    nav ul ul ul li{
      position: relative;
      top: -60px;
      left: 150px;
    }
    .show,.icon,input{
      display: none;
    }
    .fa-plus{
      font-size: 15px;
      margin-left: 40px;
    }
    @media all and (max-width: 968px) {
      nav ul{
        margin-right: 0px;
        float: left;
      }
      nav .logo{
        padding-left: 30px;
        width: 100%;
      }
      .show + a, ul{
        display: none;
      }
      nav ul li,nav ul ul li{
        display: block;
        width: 100%;
      }
      nav ul li a:hover{
        box-shadow: none;
      }
      .show{
        display: block;
        color: white;
        font-size: 18px;
        padding: 0 20px;
        line-height: 70px;
        cursor: pointer;
      }
      .show:hover{
        color: cyan;
      }
      .icon{
        display: block;
        color: white;
        position: absolute;
        top: 0;
        right: 40px;
        line-height: 70px;
        cursor: pointer;
        font-size: 25px;
      }
      nav ul ul{
        top: 70px;
        border-top: 0px;
        float: none;
        position: static;
        display: none;
        opacity: 1;
        visibility: visible;
      }
      nav ul ul a{
        padding-left: 40px;
      }
      nav ul ul ul a{
        padding-left: 80px;
      }
      nav ul ul ul li{
        position: static;
      }
      [id^=btn]:checked + ul{
        display: block;
      }
      nav ul ul li{
        border-bottom: 0px;
      }
      span.cancel:before{
        content: '\f00d';
      }
    }
    .content{
      z-index: -1;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
      text-align: center;
    }
    header{
      font-size: 35px;
      font-weight: 600;
      padding: 10px 0;
    }
    p{
      font-size: 30px;
      font-weight: 500;
    }
<div>
     <nav>
          <div class="logo">
    Davids Nav</div>
    <label for="btn" class="icon">
            <span class="fa fa-bars"></span>
          </label>
          <input type="checkbox" id="btn">
          <ul>
    <li><a href="#">Home</a></li>
    <li>
              <label for="btn-1" class="show">Features +</label>
              <a href="#">Features</a>
              <input type="checkbox" id="btn-1">
              <ul>
    <li><a href="#">Pages</a></li>
    <li><a href="#">Elements</a></li>
    <li><a href="#">Icons</a></li>
    </ul>
    </li>
    <li>
              <label for="btn-2" class="show">Services +</label>
              <a href="#">Services</a>
              <input type="checkbox" id="btn-2">
              <ul>
    <li><a href="#">Web Design</a></li>
    <li><a href="#">App Design</a></li>
    <li>
                  <label for="btn-3" class="show">More +</label>
                  <a href="#">More <span class="fa fa-plus"></span></a>
                  <input type="checkbox" id="btn-3">
                  <ul>
    <li><a href="#">Submenu-1</a></li>
    <li><a href="#">Submenu-2</a></li>
    <li><a href="#">Submenu-3</a></li>
    </ul>
    </li>
    </ul>
    </li>
    <li><a href="#">Portfolio</a></li>
    <li><a href="#">Contact</a></li>
    </ul>
    </nav>
        
         
    </div>

最佳答案

在我的浏览器中,视口(viewport)宽度为 969px,媒体查询导致 <ul>被隐藏。我更新了那个查询并在 700px 添加了另一个查询。您可以完成其余的媒体查询以获得完全响应式导航。

接下来,为了让导航栏在滚动时始终保持在视口(viewport)的顶部,添加 position: fixed连同 top: 0

试试这个。

* {
      margin: 0;
      padding: 0;
      }
    nav {
      position: fixed;
      width: 100%;
      top: 0;
      background: #1b1b1b;
      display:flex;
      justify-content:space-around;
    }
    nav:after {
      content: '';
      clear: both;
      display: table;
    }
    nav .logo{
      
      color: white;
      font-size: 27px;
      font-weight: 600;
      line-height: 70px;
      padding-left: 60px;
    }
    nav ul{
      float: right;
      margin-right: 40px;
      list-style: none;
      position: relative;
    }
    nav ul li{
      
      display: inline-block;
      background: #1b1b1b;
      margin: 0 5px;
    }
    nav ul li a{
      color: white;
      line-height: 70px;
      text-decoration: none;
      font-size: 18px;
      padding: 8px 15px;
    }
    nav ul li a:hover{
      color: cyan;
      border-radius: 5px;
      box-shadow:  0 0 5px #33ffff,
                   0 0 10px #66ffff;
    }
    nav ul ul li a:hover{
      box-shadow: none;
    }
    nav ul ul{
      position: absolute;
      top: 90px;
      border-top: 3px solid cyan;
      opacity: 0;
      visibility: hidden;
      transition: top .3s;
    }
    nav ul ul ul{
      border-top: none;
    }
    nav ul li:hover > ul{
      top: 70px;
      opacity: 1;
      visibility: visible;
    }
    nav ul ul li{
      position: relative;
      margin: 0px;
      width: 150px;
      float: none;
      display: list-item;
      border-bottom: 1px solid rgba(0,0,0,0.3);
    }
    nav ul ul li a{
      line-height: 50px;
    }
    nav ul ul ul li{
      position: relative;
      top: -60px;
      left: 150px;
    }
    .show,.icon,input{
      display: none;
    }
    .fa-plus{
      font-size: 15px;
      margin-left: 40px;
    }
    @media all and (max-width: 968px) {
      nav ul {
        display: flex;
        font-size: 1rem;
      }
      nav .logo{
        padding-left: 30px;
        width: 100%;
      }
      .show + a, ul{
        display: none;
      }
      nav ul li,nav ul ul li{
        display: block;
        width: 100%;
      }
      nav ul li a:hover{
        box-shadow: none;
      }
      .show{
        display: block;
        color: white;
        font-size: 18px;
        padding: 0 20px;
        cursor: pointer;
      }
      .show:hover{
        color: cyan;
      }
      .icon{
        display: block;
        color: white;
        position: absolute;
        top: 0;
        right: 40px;
        line-height: 70px;
        cursor: pointer;
        font-size: 25px;
      }
      nav ul ul{
        top: 70px;
        border-top: 0px;
        float: none;
        position: static;
        display: none;
        opacity: 1;
        visibility: visible;
      }
      nav ul ul a{
        padding-left: 40px;
      }
      nav ul ul ul a{
        padding-left: 80px;
      }
      nav ul ul ul li{
        position: static;
      }
      [id^=btn]:checked + ul{
        display: block;
      }
      nav ul ul li{
        border-bottom: 0px;
      }
      span.cancel:before{
        content: '\f00d';
      }
    }
    .content{
      z-index: -1;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%);
      text-align: center;
    }
    header{
      font-size: 35px;
      font-weight: 600;
      padding: 10px 0;
    }
    p{
      font-size: 30px;
      font-weight: 500;
    }
    
@media only screen and (max-width: 700px) {
    nav {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    nav .logo {
      line-height: 40px;
    }
}
<div>
     <nav>
          <div class="logo">
    Davids Nav</div>
    <label for="btn" class="icon">
            <span class="fa fa-bars"></span>
          </label>
          <input type="checkbox" id="btn">
          <ul>
    <li><a href="#">Home</a></li>
    <li>
              <label for="btn-1" class="show">Features +</label>
              <a href="#">Features</a>
              <input type="checkbox" id="btn-1">
              <ul>
    <li><a href="#">Pages</a></li>
    <li><a href="#">Elements</a></li>
    <li><a href="#">Icons</a></li>
    </ul>
    </li>
    <li>
              <label for="btn-2" class="show">Services +</label>
              <a href="#">Services</a>
              <input type="checkbox" id="btn-2">
              <ul>
    <li><a href="#">Web Design</a></li>
    <li><a href="#">App Design</a></li>
    <li>
                  <label for="btn-3" class="show">More +</label>
                  <a href="#">More <span class="fa fa-plus"></span></a>
                  <input type="checkbox" id="btn-3">
                  <ul>
    <li><a href="#">Submenu-1</a></li>
    <li><a href="#">Submenu-2</a></li>
    <li><a href="#">Submenu-3</a></li>
    </ul>
    </li>
    </ul>
    </li>
    <li><a href="#">Portfolio</a></li>
    <li><a href="#">Contact</a></li>
    </ul>
    </nav>
        
         
    </div>

关于html - 导航栏列表项未显示且导航栏未固定在顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62615318/

相关文章:

javascript - 没有互联网,Bootstrap 无法工作

python - 如何在模板中显示递归数据?

css - 如何将 sass 代码编译成另一个目录中的 css 文件?

html - 具有 3 个 div 的容器,使用 Flex-box/Grid

html - 使用 flex 将一个高的 child 放入一个矮的 parent 中

javascript - Handsontable - 隐藏特定行(按单元格值)

html - 连续放置并对齐图片

html - 如何使 html 元素不可选择,除了文本区域和输入?

javascript - window.location.href 更改后浏览器后退按钮无法正常工作

css - 元素溢出视口(viewport)并导致水平滚动