Search code examples
htmlcssflexboxnavbarnav

Navbar List Items not showing and Navbar not sticking to the top


I have my navbar and I have 2 issues. Though it seems to be responsive there's 2 issues. 1. between 968px and about 2001px the list items dont show when clicked or hovered. After around 2000px the list items show. Issue 2. I want the navbar to stick to the top of the page when scrolling down. I tried messing with the positioning but nothing was working for me . this is my code

    * {
      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>


Solution

  • On my browser at 969px viewport width, a media query is causing the <ul> to be hidden. I updated that query and added another one at 700px. You can work on finishing the rest of the media queries for a fully responsive nav.

    Next, in order to have the navbar always stick to the top of the viewport while scrolling, add position: fixed along with top: 0

    Try this out.

    * {
          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>