I am currently working on creating a navigation bar, but I'm struggling to identify the issue with making it responsive. Is there anyone who can offer assistance?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" rel="stylesheet">
*{ margin: 0; padding: 0; box-sizing: border-box; } header{ position: relative; width: 100%; height: 80px; background-color: rgb(153, 153, 153); display: flex; justify-content: space-between; align-items: center; } header a img{ width: 100px; padding-top: 20px; } header #menu-bar { list-style-type: none; } header ul li{ display: flex; list-style: none; } header ul li a{ text-decoration: none; padding: 0 10px; color: white; font-size: 18px; text-transform: uppercase; letter-spacing:1.4px; } .nav-bar{ border: 2px solid white; text-align: center; margin-right: 10px; display:none; } .fa-bars{ color: white; font-size: 18px; padding: 5px; } .active{ display: block; background:red } @media only screen and (max-width:600px){ header ul { display:none; margin-top: 200px; width: 100vw; text-align: center; background:skyblue; } .nav-bar{ display:block; } header ul li{ flex-direction: column; } header ul li a{ color: black; padding-top: 10px; } .navbar { display: none; } }
</head>

<body>
    <!-- Adding Navbar -->

    <header>
        <a href="#logo" class="logo">
            <img src="./img/Logo.png" alt="logo">
        </a>
        <ul id="menu-bar">
            <li>
                <a href="#">Home</a>
                <a href="#">About</a>
                <a href="#">Services</a>
                <a href="#">Contact</a>
            </li>
        </ul>
        <a href="" class="nav-bar" id="navBtn"><i class=" fas fa fa-bars"></i></a>
    </header>

     <script>            
      let mainNav = document.getElementById('menu-bar');
        let navBarToggle = document.getElementById('navBtn');

       <!--I am facing a problem in this line-->

        navBarToggle.addEventListener('click', function (){
            mainNav.classList.add('active');
        });
</body>
</html>

Answer №1

Take a look at the snippet provided. All functionalities appear to be operating correctly. Your navbar transforms into a hamburger-menu just as specified in your media-queries set at a max-width: 600px

For easier snippet viewing, I adjusted your media max-width to 1000px

let mainNav = document.getElementById('menu-bar');
        let navBarToggle = document.getElementById('navBtn');

       // Issue encountered with this line 

        navBarToggle.addEventListener('click', function (){
            mainNav.classList.add('active');
        });
*{
     margin: 0;
     padding: 0;
     box-sizing: border-box;
}
 header{
     position: relative;
     width: 100%;
     height: 80px;
     background-color: rgb(153, 153, 153);
     display: flex;
     justify-content: space-between;
     align-items: center;
}
 header a img{
     width: 100px;
     padding-top: 20px;
}
 header #menu-bar {
     list-style-type: none;
}
 header ul li{
     display: flex;
     list-style: none;
}
 header ul li a{
     text-decoration: none;
     padding: 0 10px;
     color: white;
     font-size: 18px;
     text-transform: uppercase;
     letter-spacing:1.4px;
}
 .nav-bar{
     border: 2px solid white;
     text-align: center;
     margin-right: 10px;
     display:none;
}
 .fa-bars{
     color: white;
     font-size: 18px;
     padding: 5px;
}
 .active{
     display: block;
     background:red 
}
 @media only screen and (max-width:1000px){
     header ul {
         display:none;
         margin-top: 200px;
         width: 100vw;
         text-align: center;
         background:skyblue;
    }
     .nav-bar{
         display:block;
    }
     header ul li{
         flex-direction: column;
    }
     header ul li a{
         color: black;
         padding-top: 10px;
    }
     .navbar {
         display: none;
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" rel="stylesheet">
</head>

<body>
    <!-- Adding Navbar -->

    <header>
        <a href="#logo" class="logo">
            <img src="./img/Logo.png" alt="logo">
        </a>
        <ul id="menu-bar">
            <li>
                <a href="#">Home</a>
                <a href="#">About</a>
                <a href="#">Services</a>
                <a href="#">Contact</a>
            </li>
        </ul>
        <a href="" class="nav-bar" id="navBtn"><i class=" fas fa fa-bars"></i></a>
    </header>
</body>
</html>

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

Utilizing Reveal and Interchange for an Adaptive Lightbox in Foundation 4

Can Foundation 4 Reveal be used with images in a way that they are only loaded when the user clicks an element to open the modal? My objective is to create an Adaptive Lightbox using Interchange. When a user clicks on a thumbnail, it should display an ima ...

Is it possible to correlate the position of a medical image segment with the position of a mouse click event in JavaScript?

Greetings and thank you for taking the time to read this! This query is closely linked to the following library: https://github.com/FNNDSC/ami I am interested in developing a function that can detect when a user clicks on a segment within an image, trigg ...

When resizing my web page, the content overlaps and the images become distorted

Although the page appears fine in normal view mode, an issue arises when the page size is reduced - the image within the body tag vanishes. Multiple attempts have been made to rectify this problem by altering the code, but unfortunately, none have proven s ...

Converting JavaScript QML objects to C++ QT components

I have a QML JavaScript function that generates and returns a component called Item: function createComponent() { var component = Qt.createComponent('MyComponent.qml'); var obj = component.createObject(container, {'x': 0, &apos ...

Creating a star-based rating feature through directive implementation

Can anyone help me figure out why my static star rating system using angularjs/ionic is not showing up on the screen? I've been struggling with it and would appreciate some guidance. service.html <ion-list> <ion-item ng-repeat="busine ...

Submitting Files to the API using Vue.js

I have the following code in Vue.js and I am trying to upload a file, but my issue is that I want to save the filename of the uploaded file to the JSON API using a POST method. Is there a way to achieve this? Thank you in advance. <div class="input-f ...

Unable to modify the value of an HTML dropdown list

My latest project is a website located at pg.wcyat.me (Source code), where I utilized github.com/thdoan/pretty-dropdowns for dropdown menus. Using JavaScript, I am able to dynamically change the values of select lists. For example: document.getElementById( ...

What could be causing the "undefined class in the imported eval() function" error

Currently, I'm exploring a unique approach to creating a replacer/reviver combination that facilitates the proper serialization and deserialization of ES6 classes for a TypeScript project I am currently developing. To accomplish this, I implemented a ...

Use the mouse scroll to navigate and scroll a vertical-rl div from right to left

Here I have created a vertical-rl environment; (function() { function scrollHorizontally(e) { e = window.event || e; var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))); document.documentElement.scrollLeft -= (delta * 50); ...

Angular2 Window Opener

Trying to establish communication between a child window and parent window in Angular 2, but I'm stuck on how to utilize window.opener for passing a parameter to Angular 2. In my previous experience with Angular 1.5, I referenced something similar he ...

Maintain the original canvas aspect ratio while resizing the browser window

I am working with a canvas that has a width and height set to 100%. It occupies the entire screen, and I have drawn an image on it. My goal is to maintain the original aspect ratio of the image when resizing the browser window. I am looking to achieve the ...

Can someone point me in the direction of the app.css file within DurandalJS that controls the modals

I am currently facing an issue with implementing a modal in my app using the DurandalJS (SPA) framework. The modal is not displaying correctly, as it opens without a blockout and stretches horizontally across the entire browser. (I expect "Feeds", the "Ne ...

React Router's nested route causes a full page reload when navigating

I have been working on setting up nested routing in React Router and here is my code: import React from 'react'; import DefaultSwitch from './components/DefaultSwitch/DefaultSwitch'; import './scss/App.scss'; const App = () ...

Detecting which item is selected when the tab key is pressed using Javascript and jQuery

I'm having trouble figuring out which element is currently selected or focused when using the tab key. I want to trigger a function when the enter key is pressed on the currently selected item. The code should be pretty straightforward, but my proble ...

Tips for showcasing the stored value from the database in a table

This problematic table was created: http://jsfiddle.net/ofd3nox3/ I am currently facing an issue with displaying the values stored in the database for this table per user. For example, a user has specified availability on Thursdays in the morning, Friday ...

Is there a way to customize the appearance of an unordered list by setting it to display as an image instead of default bullets? I want to

I have been attempting to achieve this desired outcome. However, my efforts to reproduce it resulted in the check marks being rendered at a smaller size than intended due to using an SVG file. An example of this issue can be seen in the following image: I ...

Importing script files based on certain conditions

I am trying to dynamically import script tags based on a parameter set in the URL. Here is my current approach: function getURLValues(name) { var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location ...

Tips for retrieving information from an API and displaying it in a table

I'm struggling to retrieve data (an array of objects) from an API using a Token and display them in a table using Material-UI. However, I keep encountering the following error: Uncaught (in promise) SyntaxError: Unexpected token 'A', "Access ...

Columns in Bootstrap compressed

Seeking advice regarding setting up a recruitment portal for my employer. Having trouble with the bootstrap scaffolding - columns appear squashed on smaller devices despite using xs columns. Looking for guidance to make it more responsive. Would greatly a ...

Express and Mongoose error handling improvements for updates and puts

I'm new to using Express and I need help with my code for updating a model in my router/controller. I want to update certain parameters without modifying the "create_date" parameter, but I keep encountering an error. updateFood = function(req, res){ ...