Click X button to toggle and close the dropdown menu

After successfully creating a responsive menu that toggles with a hamburger icon at 1205px resolution, I encountered an issue. Once the hamburger icon is clicked and the dropdown menu opens, there seems to be no way to close it. I am looking to add an X button for users to easily close the menu once opened. Does anyone have suggestions on how this can be achieved?

html

 <nav class="nav-box">
  <label class="label" for="toggle">
      &#9776;
  </label>
  <input type="checkbox" id="toggle">
    <ul class="f-cb">
        <!-- my menu content -->
    </ul>
 </nav>

CSS

.label {
    margin: 0 40px 0 0;
    font-size: 26px;
    line-height: 22px;
    display: none;
    color: #433F3F;
    float: right;
    width: 180px;
}

#toggle {
    display: none;
}
/* menu */

@media only screen and (max-width: 1205px) {

    #form1 > div:nth-child(3) > header > nav.nav-box > ul{
    position: absolute;
    background-color: white;
    min-width: 150px;
    box-shadow: 5px 16px 16px 8px rgba(0,0,0,0.4);
    z-index: 1;
    }

    .label {
        display: block;
        cursor: pointer;
        margin: 0 40px 0 0;
        font-size: 28px;
        line-height: 70px;
        color: grey;
        float: right;
        width: 26px;
        float: right;
    }

    #toggle:checked + .f-cb {
        display: block !important;
    }
}

@media only screen and (max-width: 760px) {

    .label {
        margin: 0 40px 0 0;
        font-size: 28px;
        line-height: 70px;
        color: grey;
        float: right;
        width: 26px;
        float: right;
    }
}

I am attempting to add a close button 'X' within the dropdown menu that appears when clicking the hamburger icon. However, I am unsure of how to implement this. Any guidance would be appreciated.

Answer №1

It seems like your approach initially may have been incorrect. A simple solution to replicating this functionality would involve incorporating some JavaScript.

  1. Here is the HTML code snippet:
<div id="header" class="border">
  <nav id="nav_container">
    <button id="menu_btn" class="cursor_pointer" type="button">
      &#9776;
    </button>
    <ul id="menu_list_container" class="border">
      <li id="close_btn_container">
        <button id="close_menu_btn" class="cursor_pointer" type="button">
&#10006;
        </button>
      </li>
      <li>Point I</li>
      <li>Point II</li>
      <li>Point III</li>
    </ul>
  </nav>
</div> 
  1. Next, here is the CSS code snippet:
* {
  box-sizing: border-box
}

#header {
  width: 100%;
  display: flex;
  justify-content: flex-end;
  padding: 10px;
  max-height: 60px
}

#nav_container {
  display: none;
  flex-direction: column;
  align-items: flex-end;
  @media only screen and (max-width: 760px) {
      display: flex;
  }
}

#menu_btn {
  width: 40px;
  min-height: 40px;
}

#menu_list_container {
  list-style: none;
  padding: 10px;
  display: flex;
  flex-direction: column;
  row-gap: 10px;
  display: none;
}

#close_btn_container {
  width: 100%;
  display: flex;
  justify-content: flex-end
}

.border {
    border: 1px black solid;
}

.cursor_pointer {
    cursor: pointer
}
  1. Finally, here is the JS code snippet:
const menuButton = document.getElementById("menu_btn")
const closeMenuButton = document.getElementById("close_menu_btn")
const menuListContainer = document.getElementById("menu_list_container")

menuButton.addEventListener("click", () => {
  menuListContainer.style.display = "flex"
})

closeMenuButton.addEventListener("click", () => {
  menuListContainer.style.display = "none"
})
  1. For reference, you can view the CodePen link here: https://codepen.io/takhirkudusov/pen/dywZrrx

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 navigate to a different section of a webpage while also jumping to a specific id within that section seamlessly?

I'm trying to create a navbar link that will take me directly to a specific section of a page, but I'm having trouble getting it to work. Here's what I've tried: <a href="home#id" class="nav-link text on-hover"></a> Where ...

Tips for expanding the boundary of a Font Awesome icon

The image is in a h1 editing margin of h1 only edits margin-left increases the margin on the left of the image. trying to increase margin of i element does nothing either on i element does nothing either. <h1 class="display-4"><i class="fas fa- ...

Updating the favicon by replacing the image URL

I'm looking to customize the favicon on resource HTML pages. Specifically, when visiting the image URL, I want my own icon to display in the title bar instead of the server's icon (in this case XAMPP). ...

Tips for updating the color scheme in your CSS file by making hue adjustments to all colors

I have a CSS file and I am looking to automatically generate multiple color variations of the file by altering the hue, similar to using the "colorize" function in GIMP. I came across a tool that does exactly what I need at , however it does not support t ...

Troubleshooting a Border Problem in Bootstrap 5 Accordions

Having trouble with a Bootstrap 5 accordion header that has an icon drop-up menu. The bottom border disappears under the icon and I can't figure out why? HTML <div class="w-50 mt-5 mx-auto"> <div class="accordion favorites-f ...

Adding external JSON data to a plain HTML document can be achieved through the process of

I have been experimenting with extracting data from an API in JSON format, but I am struggling to figure out how to convert the JSON tags into HTML elements. You can view a sample of the JSON data here. Does anyone know how to transform this JSON into DI ...

The attribute aria-required is necessary for a radio group

When creating a required radio group, how should the aria-required="true" attribute be assigned? Specifically, I have multiple <input type="radio"> elements with the same name grouped under a <fieldset>. Should the aria-required be placed o ...

Steps to ensure your Bootstrap side menu spans the entire height of the page

Welcome to my side menu <template> <div class="p-3 text-bg-dark shadow"> <div class="dropdown"> <a href="#" class=" align-items-center text-white text-d ...

The anchor for the circular image is oversized

Currently, I have a PHP code that displays a user's profile picture, and when clicked, it takes them to their own profile. The image is displayed correctly, and the link works fine. However, there seems to be an issue with the area covered by the anch ...

Issues with functionality arise when cloning HTML divs using JQuery

VIDEO I created a feature where clicking a button allows users to duplicate a note div. However, the copied note does not function like the original - it's not draggable and changing the color of the copied note affects the original note's color. ...

Grid sidebar using tailwindcss

I just started using Tailwind CSS, but I'm having trouble understanding how the grid system works! I want my layout to look like this: https://i.sstatic.net/hlHmy.png Here is what I tried, but it's not working as expected: <div class=" ...

Adding a custom class to a select2 dropdown: How can it be done?

I have customized select2 using CSS with its general classes and IDs. Currently, I am attempting to customize a specific class that will be assigned to select2 and then applied in the CSS. The problem lies not within the select itself, but within its dro ...

Using Yii2, create a button with an onclick event that executes a JsExpression

I need to submit a form with an array of elements whose number is unknown. class DynamicForm extends Model { /** var string[] */ public elements[]; } In the view where the form submission takes place, I want to include a button that triggers a Ja ...

Spooky results displayed on website from NightmareJS

Is there a way to display the output from nightmareJS onto a webpage when a button is clicked using HTML, CSS, and JS? This is my nightmareJS code: var Nightmare = require('nightmare'); var nightmare = Nightmare({ show: false}) nightmare .go ...

Using regular expressions to extract URLs from code in Python

My research involves a dataset of newspaper article links, but I've run into an issue. The links in the dataset all end with .ece extension, which is causing problems due to some API restrictions. http://www.telegraaf.nl/telesport/voetbal/buitenlands ...

Tips for creating a dropdown that autocompletes similar to the one found in Google Chrome

Looking to create a unique custom drop-down autocomplete suggestion list using HTML and CSS, similar to Chrome's design (shown below). https://i.sstatic.net/Kia2N.png In my attempts to achieve this, I utilized an absolutely positioned table beneath ...

Displaying images in Dash Python by utilizing external Javascript

Dear audience, I am looking to incorporate an image on a Python Dash webpage that is created by JavaScript code. For more details, you can refer to the documentation here: . To include this script in a static HTML page, one would typically add the followi ...

Disabling the button add-ons functionality in Bootstrap 3 for mobile devices

I am facing a challenge with my website that has a visually appealing jumbotron and an enticing email signup call to action bar. The issue arises from the fact that my site is bilingual, catering to both German and English speakers, causing confusion with ...

Header not displaying properly on website

Link: test.getfamo.us Problem: The drop-down menu is not functioning properly when hovered over due to the main box covering it. I initially avoided using percentages for height in my website design, but after facing display issues on different monitors, ...

The DXTreeview feature in AngularJS restricts the expansion of branches

My MVC5 project contains an Index.aspx file, but when I run it in VS2013, only the top branches of the tree are visible without any option to expand them. https://i.sstatic.net/0FWUB.png Resharper confirms that my references are correct. Being new to HT ...