Height-based responsive navigation bar

I recently implemented a menu on my responsive website that appears when the viewport is 714px width or less.

Upon clicking the button, a side-sliding menu emerges across the page. However, I'm facing an issue with setting the menu height to match the current viewport while preventing scrolling down.

For reference, here's what the current menu setup looks like: https://jsfiddle.net/baqcfjt1/1/

<div class="site-container-menu">
  <div class="site-pusher">
    <header class="header">

      <a href="#" class="header__icon" id="header__icon">MENU</a>

      <nav class="menu">
        <a href="#one" class="scrolly">Link 1</a>
        <a href="#three" class="scrolly"><strong>Link 2</strong></a>
        <a href="#two" class="scrolly">Link 3</a>
        <a href="#four">Link 4</a>
      </nav>

    </header>
    <div class="site-content">
      <div class="container-menu">
        <section id="header">
          <div class="headerlogo"><img src="image" /></div>
          <div class="headerlogosmall"><img src="image" /></div>
        </section>

        <section class="main">
          -content-
        </section>


      </div>
    </div>
    <div class="site-cache" id="site-cache"></div>
  </div>
</div>

CSS

.header {
  z-index: -10;
  position: absolute;
}


/* RESPONSIVE */

@media only screen and (max-width: 714px) {
  .container-menu {
    overflow: hidden;
    *zoom: 1;
  }
  /* HEADER */
  .header__logo {
    font: inherit;
    font-weight: 700;
    padding: 0 25px;
    float: left;
  }
  /* MENU */
  .site-pusher,
  .site-container-menu {
    height: 100%;
  }
  .site-container-menu {
    overflow: hidden;
  }
  .site-pusher {
    -webkit-transition-duration: 0.3s;
    transition-duration: 0.3s;
    -webkit-transform: translateX(0px);
    transform: translateX(0px);
  }
  .site-content {}
  .header {
    position: static;
    height: 66px;
    line-height: 62px;
    color: rgba(228, 91, 65, 1.00);
    background-color: #fff;
  }
  .header__icon {
    position: relative;
    display: block;
    float: left;
    padding-left: 3em;
    font: inherit;
    font-weight: 400;
    font-size: 20px;
    height: 66px;
    cursor: pointer;
  }
  .header__icon:after {
    content: '';
    position: absolute;
    display: block;
    width: 1rem;
    height: 0;
    top: 16px;
    left: 15px;
    box-shadow: 0 10px 0 1px rgba(228, 91, 65, 1.00), 0 16px 0 1px rgba(228, 91, 65, 1.00), 0 22px 0 1px rgba(228, 91, 65, 1.00);
  }
  .menu {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    background-color: #fff;
    /*    overflow-y: scroll;
        -webkit-overflow-scrolling: touch;*/
    width: 250px;
    -webkit-transform: translateX(-250px);
    transform: translateX(-250px);
    overflow: hidden;
  }
  .menu a {
    display: block;
    padding-top: 2em;
    padding-bottom: 2em;
    color: #666666;
    height: 25%;
    text-align: center;
    line-height: 40px;
    border-bottom: 1px solid #d9d9d9;
  }
  .menu a:hover {
    color: #e45b41;
  }
  .with--sidebar .site-pusher {
    -webkit-transform: translateX(250px);
    transform: translateX(250px);
  }
  .with--sidebar .site-cache {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.6);
    z-index: 9999;
  }
}

$(document).ready(function() {

  (function($) {

    $('#header__icon').click(function(e) {
      e.preventDefault();
      $('body').toggleClass('with--sidebar');
    });

    $('#site-cache').click(function(e) {
      $('body').removeClass('with--sidebar');
    });

  })(jQuery);

});

Answer №1

To accomplish this task, you can utilize viewport-percentage length units. Check out more information about it here: https://developer.mozilla.org/en/docs/Web/CSS/length#Viewport-percentage_lengths

An effective solution would be to employ the vh CSS unit and define that the body element has a height of 100vh

Refer to this demonstration on JSFiddle for a practical example: https://jsfiddle.net/r61n4y0v/

I included the following CSS declaration:

body {
  height: 100vh;
}

Make sure to verify the browser compatibility of the vh unit before implementation. You can do so by visiting this link:

http://caniuse.com/#feat=viewport-units


If you want to specify the height:100vh; rule further, you can eliminate the overflow:hidden from .site-container-menu and directly assign height:100vh to .menu:

See this updated JSFiddle for reference: https://jsfiddle.net/6won6stx/

Your menu links currently have both a defined height with height:25% and padding which is not recommended as it may result in unexpected behavior. It's suggested to replace:

<nav class="menu">
   <a href="#one" class="scrolly">Link 1</a>
   <a href="#three" class="scrolly"><strong>Link 2</strong></a>
   <a href="#two" class="scrolly">Link 3</a>
   <a href="#four">Link 4</a>
</nav>

With:

<nav class="menu">
  <ul>
    <li><a href="#one" class="scrolly">Link 1</a></li>
    <li><a href="#three" class="scrolly"><strong>Link 2</strong></a></li>
    <li><a href="#two" class="scrolly">Link 3</a></li>
    <li><a href="#four">Link 4</a></li>
  </ul>
</nav>

Remove the height:25% from the a element and add height:25vh to the li elements instead.

View the updated JSFiddle here: https://jsfiddle.net/s5gbk7y1/

Additional modifications have been made including setting the line-height property on the menu links to 25vh.

Feel free to explore the latest fiddle and inform me if it resolves your issue!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to adjust the maximum time limit for uploaded video files in Vue?

I'm facing an issue while trying to dynamically change the maximum value of a time input field in Vue JavaScript after uploading a video file. Despite setting the attribute `max` on the input element using JavaScript, I am unable to update the max val ...

Illustrate JSON API information in an HTML unordered list

I am facing a challenge with converting JSON data into an unordered list using HTML or PHP. Here is the API key I am currently working with: While I have successfully parsed the raw JSON data with the provided code snippet: <script> $.ajax({ t ...

What is the best way to determine the number of characters that will fit within the width of the document?

I am looking to create a JavaScript function using jQuery that can determine the number of characters that will fit in a single line within the browser window. While I am currently utilizing a monospace font for simplicity's sake, I would like to adap ...

Leveraging ng-hide in Angular to show or hide elements based on the state

Is there a way to utilize the ng-hide directive based on the value selected in a dropdown menu? Specifically, I am looking to display #additional-option if option C is chosen from the dropdown list. <div class="form-group"> <label class="co ...

Execute and showcase code without redundancies

I have been exploring a way to store JavaScript code within an object and execute specific parts of it upon the user clicking a button. Here's what I've come up with so far: var exampleCode = { test: "$('body').css('background ...

Tips for displaying identical tab content across various tabs using jquery or javascript

For instance, if we have tabs labeled as 1, 2, 3, 4, and 5, and a user has selected tabs 2, 3, and 4, how can we display the same content for those tabs while showing different content for the remaining ones? ...

"Utilizing d3 to parse and track variables within JSON data

To calculate the number of occurrences of s1, s2, and s0 in JSON data and use this information to plot a multiline chart with date (path of date is as follows reviews_details>>variable vf of JSON) on the X-axis versus the number of reviews (s1/s0/s2 ...

What is the process of transmitting an array of data from Angular to a controller?

Recently, I started using AngularJS alongside asp.net MVC 5. Within my Angular code, I have an array called $scope.selectedRooms = []; where I push items into it. I want to send the data from this array to the controller after checking certain conditions. ...

What is the reason for needing to refresh when submitting form data in a Node application with an HTTP POST

Code Snippet - Angular .state('studentInfo.newStudent', { url : '/new/student', templateUrl: 'students/new-student.html', controller : function($state, $http){ this.saveStudent = func ...

I attempted to verify the login through postman, but encountered an error in the process

I created the login route on the backend and tested it in Postman, but encountered this error message: https://i.stack.imgur.com/PdyCo.png Below is the code for the login route: router.post("/login", async (req, res) => { try { const user = await ...

"Utilize a pre-defined parameter in a function and pass it to the handleSubmit function

Within my component called "MyEditDataForm," I have a redux-form where I pass a submit handling function like this: const myHandleSubmit = ({ firstInputFieldName, secondInputFieldName }) => { console.log(firstInputFieldName); console.log(secondInput ...

The Jquery .change() function refreshes the results just once

I am working on a form with 3 input fields named #first, #second, and #third, along with a fourth field labeled as #theResult. <div id="addFields"> <span id="addFieldsHeader">Add The Fields</span> <table style="margin:0 auto;"> ...

Leveraging the power of animate.css library to enhance page transitions on Nuxt

I've been working on implementing the animate.css library for nuxt page transitions, but I'm encountering some issues. I have successfully added animate.css to the nuxt.config.js file and it loads without any problems. However, when I try to use ...

Creating a sidebar that remains fixed in place even as the page is scrolled down can be achieved by using CSS and positioning properties

I am looking to create a website with a specific layout design as shown in this image: https://i.stack.imgur.com/ndKcz.png The main focus is on making the sidebar (F/T container) function as a social network link, sticking to the right side of the page ev ...

I am interested in implementing a logout feature using a button in PHP

<?php session_start(); ?> <form> <input type="button" id="logout" value="logout" name="logout"> </form> After initializing the session during login, I am seeking to terminate it using a button rather than via <a href="">l ...

Executing a Function Following the Completion of getAuth() in React Firebase

I have a function that needs the user id to run properly. However, the function is executing before the getAuth process is completed. const user = getAuth() getDoc(doc(db, 'users', user.currentUser.uid)) When I try to run the above code, it thro ...

Guide: Building a Dropdown Form in Angular 2

I have a webpage with an HTML form that includes a button positioned above the form. I am interested in adding functionality to the button so that when it is clicked, a duplicate of the existing form will be added directly beneath it. This will allow for m ...

Live Edit API

Currently, I am developing an application that requires near real-time collaborative editing capabilities for documents, similar to the functionality found in Google Documents. Since I am a beginner in this area, I would greatly appreciate any information ...

Is there a way for me to display an http status code in my fetch error?

I created a React component where I am currently working on setting the state by making a network call. My goal is to eventually pass this state down to other child components, but for now, I am focused on getting everything connected properly. While atte ...

Redirecting to a different page using Jquery

Is there a way to update the status without being redirected to base_url().'/Admin/Jobs/' every time? Code: <?php if ($key['status'] === '1'): ?> <?php echo form_open('Admin/update_job_status'); ?> < ...