I'm looking to showcase the list in a navigation bar by clicking on the hamburger menu. I want to include the 'home' and 'about' text specifically

Having trouble implementing the hamburger menu functionality in my HTML/CSS project. When clicked on a shrunken screen, I want the 'Home' and 'About' text in the nav to appear stacked on top of each other. JS is causing me some difficulties here as I'm unable to make the text appear upon clicking the hamburger menu. Anyone who can help with this or even try implementing the stacking feature is appreciated. I've been striving to master working with JS in this project so any assistance would be great. Thank you!

Struggling with my HTML/CSS project where I need the 'Home' and 'About' text in the navigation to stack on top of each other when the hamburger menu is clicked on a shrunken screen. Having issues with the JavaScript part, specifically making the text visible upon clicking the menu. Will attempt to handle the stacking after sorting out the JS. Any help or suggestions on implementing the menu using JS will be highly valued, as it's a crucial aspect of this project that I'm focusing on. Have a good day.

'''''''
html
'''''''

    <!DOCTYPE html>
        <html lang="en">
          <head>
            <meta charset="UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
            <meta http-equiv="X-UA-Compatible" content="ie=edge" />
            <title>Backroads || Tour Company</title>
            <!-- favicon -->
            <link rel="shortcut icon" href="./images/favicon.ico" type="image/x-icon" />
            <!-- font-awesome -->
            <link
              rel="stylesheet"
              href="./fontawesome-free-5.12.1-web/css/all.min.css"
            />
            <link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css”>
            <!-- styles css -->
            <link rel="stylesheet" href="./css/styles.css" />
          </head>
        
          <body>
            <!-- header  -->
            <main>
        <nav class="topNav">
          <ul>
            <div>
              <li><img src="./images/favicon.ico" alt=""></li>
            </div>
            <button class="hamburger" id="hamburger" onclick="showText()">
              <i class="fas fa-bars"></i>
            </button>
            <li><a href="#home">Home</a></li>
            <li><a href="#section-title">About</a></li>
          </ul>
        </nav>
        </main>
<!-- js -->
    <script src="./js/app.js"></script>
  </body>
</html>
        
'''''''
css
'''''''
        
        /*
        =============== 
        Fonts
        ===============
        */
        @import url("https://fonts.googleapis.com/css?family=Lato:400,700&display=swap");
        
        /*
        =============== 
        Variables
        ===============
        */
        
        :root {
          /* primary/main color */
          --clr-primary-5: hsl(185, 62%, 45%);
          --clr-white: #fff;
          --transition: all 0.3s linear;
          --spacing: 0.25rem;
          --ff-primary: "Lato", sans-serif;
        }
        /*
        =============== 
        Global Styles
        ===============
        */
        
        * {
          margin: 0;
          padding: 0;
          box-sizing: border-box;
        }
        
        body {
          font-family: var(--ff-primary);
          background: var(--clr-white);
          color: var(--clr-grey-1);
          line-height: 1.5;
          font-size: 0.875rem;
        }
        ul {
          list-style-type: none;
        }
        a {
          text-decoration: none;
        }
        img {
          width: 100%;
          display: block;
        }
        
        h1,
        h2,
        h3,
        h4 {
          letter-spacing: var(--spacing);
          text-transform: capitalize;
          line-height: 1.25;
          margin-bottom: 0.75rem;
        }
        h1 {
          font-size: 3rem;
        }
        h2 {
          font-size: 2rem;
        }
        h3 {
          font-size: 1.25rem;
        }
        h4 {
          font-size: 0.875rem;
        }
        p {
          margin-bottom: 1.25rem;
          color: var(--clr-grey-5);
        }
        @media screen and (min-width: 800px) {
          h1 {
            font-size: 4rem;
          }
          h2 {
            font-size: 2.5rem;
          }
          h3 {
            font-size: 1.75rem;
          }
          h4 {
            font-size: 1rem;
          }
          body {
            font-size: 1rem;
          }
          h1,
          h2,
          h3,
          h4 {
            line-height: 1;
          }
        }
        /*  global classes */
        
        .btn {
          text-transform: uppercase;
          background: var(--clr-primary-5);
          color: var(--clr-white);
          padding: 0.375rem 0.75rem;
          letter-spacing: var(--spacing);
          display: inline-block;
          transition: var(--transition);
          font-size: 0.875rem;
          border: 2px solid transparent;
          cursor: pointer;
          box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
        }
        .btn:hover {
          color: var(--clr-primary-1);
          background: var(--clr-primary-8);
        }
        
        /* 
        =============== 
        Navbar
        =============== */
        
        /* background of navbar */
        nav ul{
        background-color: grey;
        display: flex;
        flex-direction: row;
        padding: .5rem;
        border: white solid;
        justify-content: flex-end;
        }
        
        nav ul li {
          padding: 0 .5rem;
        }
        
        /* icon image */
         nav div{
          margin-right: auto;
        }
        
        nav div li img  {
          width: 2rem;
        }
        
        .hamburger{
            background-color: transparent;
            border: 0;
            cursor: pointer;
            font-size: 20px;
            visibility: hidden;
        }
        
        nav li a{
          color: var(--clr-primary-5);
        }
        
        .hamburger:focus{
          outline: none;
        }
        
        @media screen and (max-width: 992px) {
          nav li a {
            display: none;
          }
        
          .hamburger{
            visibility: visible;
          }
        
      }

Answer №1

To ensure proper HTML structure, remember that a div element cannot be placed directly within a ul tag. Instead, nest li tags inside the ul tag and then you can include div or other elements within the li tag as needed. Below is an example using Bootstrap 5.2 CDN and Font Awesome 6.1.1 CDN.

function myFunction() {
  var element = document.getElementById("myDIV");
  element.classList.toggle("hide-ul");
}
.topNav {
  padding: 20px 40px;
  display: flex;
  height: auto;
  position: absolute;
  width: 100%;
  background-color: #00ffd729;
}

.topul {
  margin-bottom: 0px;
  justify-content: space-between;
  margin-left: 0px;
  padding-left: 0px;
  max-height: 0px;
  overflow: hidden;
  transition: all 800ms;
}

.hide-ul {
  display: block;
  margin-left: 0px;
  overflow: visible;
  max-height: 500px;
  transition: all 800ms;
}

.topul li a {
  text-decoration: none;
  color: #000;
  transition: all 800ms;
}

.hamburger {
  margin-left: auto;
  color: #000;
}

ul {
  list-style: none;
  padding-left: 0px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>
  <!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abc9c4c4dfd8dfd9cadbeb9e8599859b86c9cedfca9a">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />


</head>
<body>
    <nav class="topNav">
        <div  class="topul" id="myDIV" >
            <ul>
                <li>
                    <a href="#home">Home</a>
                </li>
                <li>
                     <a href="#section-title">About</a>
                </li> 
                <li>
                     <a href="#home">Home</a>
                </li>
                <li>
                     <a href="#section-title">About</a>
                </li>
           </ul>
      </div>
      <a href="#"  class="hamburger" id="hamburger" onclick="myFunction()">
           <i class="fas fa-bars"></i>
      </a>
    </nav>
</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

Retrieve and display the number of items in the shopping cart directly from localStorage using React

Utilizing react along with globalContext to populate a cart page by adding items that are then stored in an array within localStorage results in the following data structure being created: 0: {id: "9", title: "Apple Watch SE 40mm (GPS) - Spa ...

The browser failed to display the SVG image, and the console log indicated that the promise was rejected, with the message "false."

I'm struggling to understand why my SVG isn't showing up on the screen. The console log is displaying "false," which I believe indicates that a promise was rejected Here is the TypeScript file I am working with: export class PieChartComponent im ...

Move the text above within the footer using materialize framework

I'm facing an issue with my materialize footer's text disappearing when I set the height to 35px. Whenever I decrease the size of the footer, the text goes off the screen. How can I adjust it so that it stays visible? <footer style="positio ...

Need help making switch buttons in react-native?

Using the library found at this link has been quite successful on my iPhone, but it does not display properly on Android. It appears as shown here. ...

Develop a dynamic animation using gradient and opacity properties

I'm still getting the hang of HTML, JavaScript, and CSS but I recently made some changes to someone else's code to animate a gradient. The original code used background: rgb, but I switched it to background: rgba. It seems to be working fine, but ...

Avoiding overlapping of div elements in a stacked div layout

I have a challenge with creating a vertical layout of stacked divs. Everything was going smoothly until I encountered the #resume div, which stubbornly stays hidden beneath the table-containing div above it. Despite trying various adjustments to float and ...

What is the best way to reference a div link from a different PHP file?

Struggling to access a page div from another php file, I've tried calling it using (found online) admin.php#changepass Here's an example of my HTML code: <div id="changepass" class="w3-container city" style="display:none"> <form name ...

Is it possible to reset a form without using JavaScript? (The input type=reset function is not functioning correctly

So, basically I need a quick way to clear all the fields in my form. I've experimented with a couple of options: <input type="reset" value="Clear all fields"> And <button type="reset">Clear all fields</button> Unfortunately, none ...

Is it recommended for the main, module, and browser properties in package.json to reference the minified version or the source

When developing a JavaScript library, it is important to consider the structure in which it will be published. This typically includes various bundles such as CJS, ESM, and UMD, each with their own source, minified, and map files. my-package\ dist& ...

What is the best way to organize a json based on numerical values?

Can anyone guide me through sorting a JSON data into an array based on numeric value, and then explain how to efficiently access that information? {"362439239671087109":{"coins":19},"178538363954003968":{"coins":18},"234255082345070592":{"coins":137}} Th ...

Exploring the nesting of client components in Next.jsIf you are

Exploring the realm of NextJS and React, I find myself delving into the realm of client components. One such client component I'm working with is called Form.jsx. It looks something like this: export default function FormHome() { ... a plethora of ...

Obtain the count of unique key-value pairs represented in an object

I received this response from the server: https://i.stack.imgur.com/TvpTP.png My goal is to obtain the unique key along with its occurrence count in the following format: 0:{"name":"physics 1","count":2} 1:{"name":"chem 1","count":6} I have already rev ...

The submission of the form with the ID "myForm" using document.getElementById("myForm").submit() is

<form name="formName" id="formName" action="" method="post" autocomplete="off"> <input type="hidden" name="text1" id="text1" value='0' /> <input type="button" name ="submit" onClick="Submit()" value="submit"> ...

Feeling lost and frustrated trying to align a div with a menu in the center

I've been struggling for over an hour now to center a menu on my website. Despite trying all of Google's recommendations, I still can't seem to get it right. Could someone please point out what obvious mistake I might be making? Below is ...

Trigger the C# Click event with JavaScript fire function

Hi there, I could use a bit of assistance. My goal is to allow users to log in by hitting the "Enter" key on their keyboard. While I've successfully detected when the "Enter" key is pressed in my JavaScript code, I'm struggling with how to call m ...

Guide on displaying JSON data from a server on a webpage using Google Charts in real-time

Although I am not a JavaScript developer or an expert in AJAX, I consider myself a novice desktop developer. I would greatly appreciate it if you could guide me on how to connect an MCU that returns JSON data to a web page using Google gauge, running on th ...

When using a React Router path variable along with other paths, React may have difficulty distinguishing between them

Setting up react router in my project to differentiate between user username variables and other paths is proving challenging. For instance: baseUrl/admin baseUrl/daniel Currently, React is unable to distinguish between the two. My intention is to query ...

Knex is requesting the installation of sqlite3, but I am actually utilizing a MySQL database

While attempting to execute a query on my local MySQL database using knex, I encountered an issue where it prompted me to install the SQLite3 driver, even though my database is MySQL. To troubleshoot, I installed the SQLite3 driver to see if it would reso ...

Tips for extracting popular song titles from music platforms such as Hungama or Saavn

I am looking to retrieve the names of the top trending songs/albums from platforms such as Hungama or Saavn. I experimented with web scraping packages available on npm to extract data from websites, including cheerio, jsdom, and request. Eventually, I came ...

"Vue.js integrates seamlessly with Tracking.js for advanced tracking

Has anyone successfully integrated the tracking.js library into a vueJS application? I followed these steps to install the package: npm install --save tracking After that, I defined the library in my main.js file like this: import tracking from 't ...