Troubleshooting CSS navigation bar hamburger dilemma

Currently, I am facing an issue with a toggle button on my website. When I click on the hamburger menu, the navigation bar does not show up even though the toggle feature is working perfectly fine. I have tried combining different sources to solve this problem since the underlying concept remains the same.

References:

https://www.example.com/toggle-button-video1

https://www.example.com/toggle-button-video2

* {
          margin: 0;
          padding: 0;
        }

        #toggle {
          display: none;
        }

        .hambuger {
          z-index: 1;
          position: fixed;
          width: 25px;
          top: 20px;
          left: 28px;
          cursor: pointer;
        }

        .line {
          display: block;
          width: 100%;
          height: 3px;
          margin-bottom: 3px;
          background-color: black;
        }

        .menu {
          display: none;
          width: 70px;
          line-height: 1.7em;
          margin: 40px;
        }

        .menu a {
          display: block;
          text-decoration: none;
          color: black;
          border: 1px grey solid;
        }

        #toggle:checked~.menu {
          display: block;
        }
<header class="content">
          <label for="toggle" class="hambuger">
            <input type="checkbox" id="toggle">
            <span class="line"></span>
            <span class="line"></span>
            <span class="line"></span>
          </label>

          <nav class="menu">
            <a href="#" class="active">Home</a>
            <a href="#" class="active">About us</a>
            <a href="#" class="active">History</a>
            <a href="#" class="active">Contacts</a>
          </nav>
        </header>

Answer №1

To use the general sibling selector ~ to choose your .menu item from the input, I made the decision to relocate the input so that it becomes a sibling with the menu within the same parent (the header element).

* {
  margin: 0;
  padding: 0;
}

#toggle {
  display: none;
}

.hambuger {
  z-index: 1;
  /*keeps displaying the icon when it clicks*/
  position: fixed;
  width: 25px;
  /*The parents (hamburger) of siblings (span)*/
  top: 20px;
  left: 28px;
  cursor: pointer;
  /*to able to click it*/
}

.line {
  display: block;
  /*to show the three lines*/
  width: 100%;
  /*Maximize the width of the parents (hamburger: 25px width)*/
  height: 3px;
  margin-bottom: 3px;
  /*to separate and see the icons*/
  background-color: black;
  /*color of the icons*/
}

.menu {
  display: none;
  width: 70px;
  line-height: 1.7em;
  margin: 40px;
}

.menu a {
  display: block;
  text-decoration: none;
  color: black;
  border: 1px grey solid;
}

#toggle:checked~.menu {
  display: block;
  /*The toggle became the parents while menu is the sibling here*/
}
<header class="content">
  <input type="checkbox" id="toggle">
  <label for="toggle" class="hambuger">
    <span class="line"></span>
    <span class="line"></span>
    <span class="line"></span>
  </label>
  <nav class="menu">
    <a href="#" class="active">Home</a>
    <a href="#" class="active">About us</a>
    <a href="#" class="active">History</a>
    <a href="#" class="active">Contacts</a>
  </nav>

</header>

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

Styling with CSS or using tables: What should you choose?

Is there a way to replicate the frame=void functionality in CSS, or will I need to manually add frame=void to each table I create in the future? <table border=1 rules=all frame=void> I attempted to search for a solution online, but unfortunately, m ...

Accessing a JavaScript variable in another <script> section

I'm facing a challenge where I need to access a JavaScript variable that is declared in one block of an HTML page from another block on the same page. This is crucial for me to be able to halt an AJAX call that is currently ongoing. However, I'm ...

Accessing iframe's HTML using a server-side solution by circumventing the Cross-Domain Policy

Our current challenge involves accessing dynamically generated HTML elements using JavaScript, particularly when trying to extract image URLs from these elements. When the HTML elements are generated solely through JavaScript, extracting the URL is straigh ...

Unable to properly access required file path through HTML source

I have a confidential folder named 'inc' where I store sensitive files such as passwords in php connection files. This folder is located at the same level as the 'public_html' folder. I successfully accessed php files with database conn ...

Error: jQuery is unable to access the property 'xxx' because it is undefined

While attempting to make a post request from the site to the server with user input data, I encountered an error message saying TypeError: Cannot read property 'vehicle' of undefined as the response. Here is the HTML and script data: <!DOCTY ...

How can one retrieve the 'alt' attribute text from an image tag on a webpage using the JSOUP library in an Android application?

Extracting the 'alt' text from an 'img' tag in Android after clicking a button is causing issues. The attempted code is not working as expected. Here is the snippet of the code that was used: Document doc = Jsoup.connect(url).get(); ...

Reset the queue for the slideDown animation using jQuery

I am experiencing an issue with my code where multiple animation effects are running simultaneously. Specifically, when I hover over navbarli1 and then move to another li element with the same navbarli1 class, the slide-down effect is not working as expect ...

Trouble encountered with card flip style login form in Vue.js, as the card is not maintaining its position properly during transition animations

Need help styling a login/signup component in Vue.js where flipping between the two cards isn't working smoothly. The transition is causing one card to move down and right while the other comes in from the bottom right. It's hard to explain the i ...

Expanding the padding of the selected item to create more breathing room

Upon examining the following: https://jsfiddle.net/h8kmove5/27/ Let's say you select 9 at the bottom. The display shows 8 9 10. My intention is to create some additional space on either side of 9, or any other active item for that matter. This way, ...

Automatically updating the results section while executing SQL queries in PHP

Here is a JavaScript/Ajax code snippet: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready (function () { var updater = se ...

Ensuring that a header one remains on a single line

Within this div, I have set the width and height to be 250px. Specifically, the h1 element inside the div has a height of 50px. Users who are updating content on my website can input any text they desire within this box. However, when the text within the ...

What steps do I need to take in order to accomplish this specific CSS

Hey there, I have a question that may seem silly, but I am struggling to find a solution on my own. Unfortunately, my CSS skills are not the best :( I'm trying to achieve the following layout where the text should wrap at the end of the container (di ...

Invoke the session on a different page and incorporate it into your JavaScript code

My php files, ajax.php and index.php, contain a mix of php code, html, and javascript. I am developing a quiz website where questions are retrieved from a database using sql in ajax.php and displayed on index.php through an ajax method. The user's sco ...

What is the best way to capture a screenshot of a website using its URL?

I've been curious about the possibility of capturing a screenshot of a remote website using only a URL and some JavaScript code. This functionality is often seen on bookmarking sites. I can't help but wonder if this is achieved through a virtual ...

A comprehensive guide on how to find keywords within an array and then proceed to make all text within the parent div tag stand

I am currently looking for a webpage that displays a list of products based on keywords from an array. Once it detects any word in the array, it highlights it with a red background - everything is working smoothly so far. However, I now wish for the script ...

Identify the name in the list by highlighting it when its content matches the text in a specific div id

I have an unordered list structured like this: <ul> <li id="projectrow_364"> <span>Forest</span> </li> <li id="projectrow_365"> <span>Life</span> ...

How to dynamically add a form to your website

Looking to dynamically insert a form on a page based on the value selected from a datalist, all without having to reload the entire page. <form action='#' method='get'> <input list="category" name="category"> <da ...

Display two images consecutively within a single img tag

My goal is to display two images using a single <img /> tag. The first small image will be loaded and shown using the src attribute, while the second large image will be contained within the data-src attribute. Only one image will be displayed at a t ...

The onbeforeunload event should not be triggered when the page is refreshed using a clicked button

Incorporated into my webpage is a sign-up form. I would like it to function in a way that if the user accidentally refreshes, closes the tab, or shuts down the browser after entering information, a specific message will appear. <input type="text" place ...

Ajax calls within nested functions are not being executed in the correct sequence

Currently, I am utilizing the geonames API to retrieve geographical information. My objective is to extract the names of states within a country using the getStateInfo function and subsequently obtain a list of cities in each state through the getCityInfo ...