skip to Main Content

I want a drop down nav bar that (with the header) covers the entire viewport. I do not want the content behind the nav drop down to scroll behind the drop down.

I’ve achieved this by using position: fixed on the header and nav elements. And when my menu button is clicked the body has overflow: hidden applied.

Problem is, on very small viewports the bottom nav list items fall outside the viewport and I can’t scroll down to see/click them.

Here’s my code.

header {
    height: 60px;
    background-color: green;
}

body.nav-shown {
      overflow: hidden;
}

body.nav-shown header {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
}

body.nav-shown nav {
      position: fixed;
      top: 60px;
      left: 0;
      right: 0;
      min-height: calc(100vh - 60px);
      background-color: black;
      color: white;
      font-size: 30px;
      line-height: 3em;
      overflow: scroll;
}
<body class="nav-shown">

<header>
    
    <button class="nav-button">Menu</button>

    <nav>
        <ul>
      <li>Link1</li>
      <li>Link2</li>
      <li>Link3</li>
      <li>Link4</li>
      <li>Link5</li>
      <li>Link6</li>
      <li>Link7</li>
       <li>Link8</li>
      <li>Link9</li>
   </ul>
    </nav>

</header>

2

Answers


  1. Fixed section should be set with width, height and top, bottom properties, otherwise it won’t recognise its size and position.

    In this case you can use max-height: 100%; to the nav.

    header {
      height: 60px;
      background-color: green;
    }
    
    body.nav-shown {
      overflow: hidden;
    }
    
    body.nav-shown header {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
    }
    
    body.nav-shown nav {
      position: fixed;
      top: 60px;
      left: 0;
      right: 0;
      min-height: calc(100vh - 60px);
      max-height: 100%;
      background-color: black;
      color: white;
      font-size: 30px;
      line-height: 3em;
      overflow: scroll;
    }
    <body class="nav-shown">
      <header>
        <button class="nav-button">Menu</button>
        <nav>
          <ul>
            <li>Link1</li>
            <li>Link2</li>
            <li>Link3</li>
            <li>Link4</li>
            <li>Link5</li>
            <li>Link6</li>
            <li>Link7</li>
            <li>Link8</li>
            <li>Link9</li>
          </ul>
        </nav>
      </header>
    </body>
    Login or Signup to reply.
  2. You can give overflow: auto on the header element and set min-height to header element instead of nav element. Here is the updated code. Try that:-

     header {
            height: 60px;
            background-color: green;
        }
    
        body.nav-shown {
            overflow: hidden;
        }
    
        body.nav-shown header {
              position: fixed;
              top: 0;
              left: 0;
              right: 0;
              overflow: auto;
              min-height: calc(100vh - 60px);
        }
    
    
    
        body.nav-shown nav {
             position: absolute;
              top: 60px;
              left: 0;
              right: 0;
              background-color: black;
              color: white;
              font-size: 30px;
              line-height: 3em;
        }
        <body class="nav-shown">
    
        <header>
            
            <button class="nav-button">Menu</button>
    
    
            <nav>
                <ul>
              <li>Link1</li>
              <li>Link2</li>
              <li>Link3</li>
              <li>Link4</li>
              <li>Link5</li>
              <li>Link6</li>
              <li>Link7</li>
               <li>Link8</li>
              <li>Link9</li>
           </ul>
            </nav>
    
    
        </header>
    
        </body>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search