The @media CSS selector seems to be malfunctioning as it does not respond to changes in screen size

I've been struggling to implement a burger menu button that appears in the right-hand corner of the screen once it reaches a certain size (960px). However, when I resize the window, the button disappears.

index.html :

<!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>scroll website</title>
    <link rel="stylesheet" href="style.css" type="text/css">  
</head>
<body>
    <!-- Navbar section -->
    <nav class="navbar">
        <div class="navbar__container">
            <a href="#home" id="navbar__logo">COLOR</a>
            <div class="navbar__toggle" id="mobile-menu">
                <span class="bar"></span>
                <span class="bar"></span>
                <span class="bar"></span>
            </div> 
            <ul class="navbar__menu">
                <li class="nav__item">
                    <a href="#home" class="navbar__links" id="home-page">Home</a>
                </li>
                <li class="nav__item">
                    <a href="#about" class="navbar__links" id="about-page">About</a>
                </li>
                <li class="nav__item">
                    <a href="#services" class="navbar__links" id="services-page">Services</a>
                </li>
                <li class="navbar__btn">
                    <a href="#sign-up" class="button" id="signup">Sign Up</a>
                </li>
            </ul>
        </div>
    </nav>
        
</body>
</html>

style.css : (if you comment out 'top: -1000px' in the media query, the menu screen will drop down)

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Kumbh Sans', sans-serif;
    scroll-behavior: smooth;
}

.navbar {
    background: #131313;
    height: 80px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2rem;
    position: sticky;
    top: 0;
    z-index: 998;
}

.navbar__container {
    display: flex;
    justify-content: space-between;
    height: 80px;
    z-index: 1;
    width: 100%;
    max-width: 1300px;
    margin: 0 auto;
    padding: 0 50px;
}

#navbar__logo {
    background-color: #ff8177;
    background-image: linear-gradient(to top, #ff0844 0%, #ffb199 100%);
    background-size: 100%;
    -webkit-background-clip: text;
    -moz-background-clip: text;
    -webkit-text-fill-color: transparent;
    -moz-text-fill-color: transparent;
    display:flex;
    align-items: center;
    cursor: pointer;
    text-decoration: none;
    font-size: 2rem;
}

.navbar__menu {
    display: flex;
    align-items: center;
    list-style: none;
}

.navbar__item {
    height: 80px;
}

.navbar__links {
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 125px;
    text-decoration: none;
    height: 100%;
    transition: all 0.3s ease;
}

.navbar__btn {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 1rem;
    width: 100%;
}

.button {
    display: flex;
    justify-content: center;
    align-items: center;
    text-decoration: none;
    padding: 10px 20px;
    height: 100%;
    width: 100%;
    border: none;
    outline: none;
    border-radius:4px;
    background: #833ab4;
    background: -webkit-linear-gradient(to right, #fcb845, #fd1d1d, #833ab4);
    background: linear-gradient(to right, #fcb845, #fd1d1d, #833ab4);
    color: #fff;
    transition: all 0.3s ease;
}


.navbar__links:hover {
    color: #9518fc;
    transition: all 0.3s ease;
}



@media screen and (max-width: 960px) {
    .navbar__container {
        display: flex;
        justify-content: space-between;
        height: 80px;
        z-index: 1;
        width: 100%;
        max-width: 1300px;
        padding: 0; 
    }

    .navbar__menu {
        display: grid;
        grid-template-columns: auto;
        margin: 0;
        width: 100%;
        position: absolute;
        top: -1000px;
        opacity: 1;
        transition: all 0.5s ease;
        z-index: -1;
        background: #131313;
        
    }

    .navbar__menu.active {
        background: #131313;
        top: 100%;
        opacity: 1;
        transition: all 0.5s ease;
        z-index: 99;
        height: 60vh;
        font-size: 1.6rem;
    }

    #navbar__logo {
        padding-left: 25px;
    }

    #navbar__toggle .bar {
        width: 25px;
        height: 3px;
        margin: 5px auto;
        transition: all 0.3s ease-in-out;
        background: #fff
    }

    .navbar__item {
        width: 100%
    }

    .navbar__links {
        text-align: center;
        padding: 2rem;
        width: 100%;
        display: table;
    }

    .navbar__btn {
        padding-bottom: 2rem;
    }

    .buttom {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 80%;
        height: 80px;
        margin: 0;
    }

    #mobile-menu {
        position: absolute;
        top: 20%;
        right: 5%;
        transform: translate(5%, 20%);
    }

    .navbar__toggle .bar {
        display: block;
        cursor: pointer;
    }

   
}

I would greatly appreciate any assistance as I have exhausted all possible solutions without success.

Answer №1

The reason for the lack of visibility

<div class="navbar__toggle" id="mobile-menu">
   <span class="bar"></span>
   <span class="bar"></span>
   <span class="bar"></span>
</div> 

There is no content visible at this location.

To improve visibility, consider adding an icon or text inside the div element, or filling in the spans with content. Keep in mind that the default text color is black, which may be blending with a dark background causing it to not be seen clearly. Adjusting the color will help enhance visibility.

Answer №2

At this point, you haven't taken any action. Start by adding a navbar and then include a script. The example below uses your classlist to toggle:

document.getElementById('mobile-menu').addEventListener("click", function(e) {
  this.classList.toggle('open');
  const navbar = document.querySelector('.navbar__menu');
  navbar.classList.toggle('active');
});
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Kumbh Sans', sans-serif;
  scroll-behavior: smooth;
}
// Rest of the CSS code here...
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<body>
  <!-- Navbar section -->
  // Rest of the HTML code here...
</body>

Full code:

<!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>scroll website</title>
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
      font-family: 'Kumbh Sans', sans-serif;
      scroll-behavior: smooth;
    }
    
    .navbar {
      background: #131313;
      height: 80px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 1.2rem;
      position: sticky;
      top: 0;
      z-index: 998;
    }
    
    .navbar__container {
      display: flex;
      justify-content: space-between;
      height: 80px;
      z-index: 1;
      width: 100%;
      max-width: 1300px;
      margin: 0 auto;
      padding: 0 50px;
    }
    
    // Rest of the CSS code here...
  </style>
</head>

<body>
  <!-- Navbar section -->
  // Rest of the HTML code here...
  
</body>

<script>
  document.getElementById('mobile-menu').addEventListener("click", function(e) {
    this.classList.toggle('open');
    const navbar = document.querySelector('.navbar__menu');
    navbar.classList.toggle('active');
  });
</script>

</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

Using input masking to restrict user input to only numbers and English alphabet characters

My input field is masked as "999999999" and functions correctly with an English keyboard. However, I am able to enter Japanese/Chinese characters into it, which breaks the masking. Is there a way to limit input to English keyboard numerics only? <inpu ...

PHP/HTML code being disrupted by the improper use of quotation marks

Trying to update some inherited code is proving tricky due to the use of quote marks. Here's the original snippet: <a href='<?php echo SITE_URL.'members-search.html';?>' class='btn btn-large<?php if($curPage == "F ...

I made a mistake with my git repository. Should I try to fix it or just start fresh?

After spending months learning web development, I recently completed my first project. Unfortunately, right before deployment, I made a mistake with my backend git tree. I attempted to resolve the issue using suggestions from other resources, but none of ...

Including a cancel button to close the open window

var messagebox = Ext.widget("messagebox", { target: grid, progressMessage: "Loading" }); The message box displayed above indicates the loading progress bar that appears when the download button is clicked. I am looking to incorporate a cancel button i ...

AJAX issue: variable returning '0' instead of the necessary data for sending

Recently, I've been experimenting with AJAX to pass a variable from a JavaScript file and trigger a PHP file in Wordpress. Despite successfully connecting to the PHP file, the variable being sent over seems to always hold the value "0". I've trie ...

modify the class's CSS style

Hey there! I'm having an issue with my code snippet. My goal is to change the background color of both fields with the class 'navicon', as shown in the function fu(). However, it's not working as expected. Changing the color based on id ...

Using Reactjs to Send Emails

Trying to incorporate the SmptJs API for email sending using JavaScript has been quite successful in a simple HTML setup. However, I am facing challenges when attempting to integrate it into a ReactJs component! The API documentation and link can be found ...

What prevents certain scenarios from being encapsulated within a try/catch block?

Just attempted to handle ENOENT by using a naive approach like this: try { res.sendFile(path); } catch (e) { if (e.code === 'ENOENT') { res.send('placeholder'); } else { throw e; } } Unfortunately, this method is ineffectiv ...

Retrieving information from a JSON file results in the value of "undefined"

Currently, I am working on a data visualization project in React using Recharts. The data is sourced from a static JSON file. My aim is to enable users to click on the Bar Chart and have the selected value passed to the app. Subsequently, this value should ...

The parameter in a JavaScript for loop

I'm struggling with my Javascript skills. Here is the code snippet I am currently working with: var information = [ {"Column":"","keyw":"","Column_3":"day of march 2011 to someother numeric" ...

In what way do Tumblr and Google+ creatively arrange images like a jigsaw puzzle?

I'm interested in creating a unique gallery of images and I'm curious about how to stack them similar to Google and Tumblr. What I mean is, on an archive page of Tumblr, the images are stacked in columns like this: However, I also want the imag ...

Having trouble with centering a div in IE8?

Although I've never had issues with centering divs before, I'm facing a challenge in achieving 100% height columns (which seems to work on most browsers). It appears that the 100% height is causing problems with centering the div in IE8. Current ...

Adjust rankings based on the number of upvotes received by a project

I'm facing a challenge with ranking projects based on the number of votes they receive. No matter the vote count, the project always ends up getting ranked as 1. To address this issue, I developed a function to manage the rank count and a return hand ...

Incorporate Material Design Lite and AMP into your Angular 5 project for a sleek

Looking to develop an e-commerce progressive web app using angular 5. Wondering how to incorporate AMP with angular 5 in Google Material Design Lite. If not viable, what are some alternative options worth considering? ...

Vetur's "go to definition" feature is unable to redirect users after pressing alt and clicking

I consider myself a proficient user of Vue and I require the ability to ALT+click (multiCursorModifier) on different Vue components in my project to navigate to their definition/implementation. I have already installed Vetur and configured the following se ...

trigger the C# event handler and then follow up with the execution of a JavaScript function

I want to discuss the order of execution between a JavaScript function and a C# method: Here is the code for a button in HTML: <asp:Button ID="MyBut" runat="server" OnClick="MyBut_Click" CssClass="MyBut" /> In C#: protected void MyBut_Click(obj ...

Tips for loading images from the local file system in Angular JS

As a beginner with AngularJS, I am looking to display an image from a sub-folder. Here is my controller code: $scope.img_source = "../WebContent/Welcom2Iquote.gif"; This is how the code looks in my index.html: <img ng-src="{{img_source}}"/> ...

When the user signs in with Next-auth, they will be redirected to /signin with

After following the documentation to implement next-auth, I encountered an issue. When I visit http://localhost:3001/api/auth/signin, I see a page with a link (https://i.stack.imgur.com/xb0fx.png) but clicking "signin with Google or GitHub" just refreshes ...

Show each individual layer from a KMZ file on a map with the ArcGIS API for JavaScript

Can I selectively show specific layers from a kmz file on my map? My current setup involves using the ArcGIS API for JavaScript. Specifically, I want to display only folders related to the current day outlook from a KMZ file provided by the NWS. There are ...

"Substitute the dropdown arrow in the select menu with a font awesome

I am currently facing a challenge in my project where I need to customize the select dropdown arrow by replacing it with a FontAwesome icon (specifically chevron-circle-down). While I have found resources on how to replace the background of the dropdown ...