Having trouble with the burger menu not opening while using javascript

Whenever I click the burger_btn, it changes to cross_btn. however, the list menu fails to appear on the screen. In an attempt to debug this issue, I inserted console.log() statements at the beginning and end of both functions. Surprisingly, the console.log() messages were being displayed in the console without any errors...

document.addEventListener("DOMContentLoaded", function() {
    const burger_btn=document.querySelector("#burger");
    const cross_btn=document.querySelector("#cross");
    const list=document.querySelector(".header ul");
    burger_btn.addEventListener('click',show);
    cross_btn.addEventListener('click',hide);

    function show(){
        burger_btn.style.display="none";
        cross_btn.style.display="block";
        list.classList.add("open");
    }
    function hide(){
        burger_btn.style.display="block";
        cross_btn.style.display="none";
        list.classList.remove("open");
    }

});
*{
    margin:0;
    padding:0;
    font-family:'Poppins',Arial, Courier, monospace;
}
:root{
    --blackish:#1f2833;
    --creamish:#fffdd0;
}
.open {
    display:block;
    position: absolute;
    background-color: var(--blackish);
    color: var(--creamish);
    width: 100%;
    height: 25rem;
    text-align: center;
    font-size: 5rem;
}

@media only screen and (max-width:360px){
    .header ul{
        display:none;
    }
    .list #burger{
        display:block;
    }
}
<link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
        <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" rel="stylesheet">

<div class="header white" id="header">
<div class="header white" id="header">
        <div class="logo">
            <a href="#"><img src="/logo/logo 1.png" alt="logo"></a>
        </div>
        <div class="list">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Blog</a></li>
                <li><a href="#">Work</a></li>
                <li><a href="#">Reviews</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
            <i id="burger" class="fa-solid fa-bars"></i>
            <i id="cross" class="fa-solid fa-xmark"></i>
        </div>
    </div>

Answer №1

The reason for this issue lies in the concept of CSS Specificity

Your CSS rule for .open is being overridden by the more specific selector .header ul.

To resolve this, you can update the selector from .open to .header ul.open.

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

The resize function fails to trigger when it is required

Struggling to get this code working properly. If the window width is greater than 800, I want 6 images with a red background. If the window width is less than 800, I want 4 images with a blue background. I need this functionality to work both on r ...

What is the best way to utilize Selenium for choosing an item from a drop-down menu?

My current challenge involves using Selenium to interact with a dropdown menu in Python. The HTML snippet below shows the structure of the website I'm working on. <select data-id="1388874259461-k9k0y" name="walletselectedbitcoin" id="selectWallet" ...

How can I use jQuery to reload the page only at certain intervals?

I'm currently using jQuery to reload a page with the code provided below: <script type="text/javascript"> $(document).ready(function(){ setInterval(function() { window.location.reload(); }, 10000); }) & ...

employing iframes dynamically to overlay a webpage

I inserted this iframe into a webpage <iframe src='http://redbug.redrocksoftware.com.au:80/Pages/chamara.html' style="position:absolute;z-index:1;" ></iframe> The chamara.html page has a button that, when clicked, should cover the c ...

Utilizing a variable as a condition in the results

Currently, I am utilizing this Moongose query to work with geo-spatial data: Locations.find({ loc: { $geoWithin: { $centerSphere: [[lng, lat], radius / 6378.1], }, } }, cb); While it functions effectively, I am curious ...

choose the text following a specific section within the class attribute of an element

In my code, I am trying to extract a number from an input text element using the jQuery method find. The class name of the input text contains a variable number denoted as x, following the substring page-id. Here is the approach I have taken: var id = ui ...

Applying CSS to Dynamic JavaScript Elements: A Guide to Styling Without In-line Code

I'm a beginner in web development and I've been experimenting with applying styles to dynamically generated div elements using JavaScript. So far, I've only been able to manually add inline styling through this function: function applyStyle( ...

Using Leaflet to set the view based on a GeoJSON dataset

I am working on incorporating a leaflet.js map with a square shape imported from an external GeoJSON file. I have determined the exact coordinates for the center of the square, and now I need to convert these coordinates in order to pass them correctly t ...

Having trouble with autoescaping not working properly on your Django test project display?

Seeking ways to enhance project performance: Code snippet from myworld/members/view.py: def testing(request): template = loader.get_template('template.html') context = { 'heading': 'Hello &lt;i&gt;my&lt;/i&gt; ...

Fixed positioning within a container's bounds using CSS

I am trying to achieve a text fade effect at the bottom of a div with content. To do this, I am using position: fixed; within a div that has overflow-y:scroll. However, I am facing an issue where the div that is supposed to give the fade effect does not a ...

Using jQuery to set menu active states for a sprite-based navigation with distinct class names

I'm curious about how to accomplish this using JQuery. I have a menu where each item must have a unique class name in order for the CSS to properly display a sprite background position shared by all items. When an item is clicked, I also need to activ ...

Is it possible for me to activate a function on a different component using my header?

Hi there, I'm curious about how Vue routing and the tree structure work together. In my setup, I have a parent router that contains both my router-view and header at the same level. I want to be able to trigger some functions from my header component ...

Tips for accessing a value in a multidimensional json array without the key in JavaScript/jQuery?

I'm working with a JSON file that contains a multidimensional array. The array consists of city data at the first level and temperature data at the second level. I'm facing difficulties in dynamically extracting values from the second level. I a ...

react component fails to rerender upon state change

I am struggling with a React functional component that includes a file input. Despite selecting a file, the text in the h1 tag does not change from choose file to test. The handleChange function is triggered successfully. A console.log statement confirm ...

Is Riot.js the best choice for server-side rendering and routing?

Currently, I am using Node along with Riot.js and the Grapnel routing library. I have successfully set up routing on the client side, but I am facing difficulty in making it work on the server side. The functioning of my client router is straightforward. ...

Error: jQuery JSONP syntax error due to missing semicolon before statement

Currently, I have implemented the following code to access a rest service that is hosted on a different domain: $.ajax({ type: 'GET', url: url, async: false, jsonpCallback: 'jsonCallback' ...

Performing a JavaScript Axios POST request following a series of iterations using a while loop with

Just getting started with async/await and feeling a bit lost. I'm trying to figure out how to send an axios post request after a while loop finishes. Is there a way to wrap the while loop in an async function and await for it? Here's the code s ...

Is there a way to deactivate the click function in ngx-quill editor for angular when it is empty?

In the following ngx-quill editor, users can input text that will be displayed when a click button is pressed. However, there is an issue I am currently facing: I am able to click the button even if no text has been entered, and this behavior continues li ...

Jose authentication is encountering issues with verifying JWT

My Next.js/Clerk.js middleware setup looks like this: import { authMiddleware } from "@clerk/nextjs"; import { jwtVerify } from "jose"; export default authMiddleware({ publicRoutes: ["/", "/contact", "/pricin ...

Is it possible to bind HTML within an AngularJS partial but encountering an inexplicable issue?

I'm facing an issue with my ng-repeat that involves using a partial HTML file to loop through and display items on a page. The variables within the partial are not displaying, but interestingly enough, the variable {{rule.name}} outside of the partial ...