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

Is there a simple and efficient method to transition the loading of a regular website to AJAX for the entire site?

Is it feasible to easily and quickly change all links on a website to load the URL that the user clicked via AJAX, rather than refreshing the webpage? My website currently has standard links, but in order to create a mobile application for iPhone, I need ...

Can an icon be included in Material UI's DataGrid headers when the sorting direction is not defined?

In the DataGrid's API of Material UI, you can see how to include a sort icon for ascending and descending directions. By default, these icons are shown as arrow up and arrow down symbols but can be customized using props. However, my project requires ...

Remove HTML element and launch in a separate browser tab while preserving styles

I am currently working on developing a single-page application with Polymer3 (Javascript ES6 with imports). One of the key functionalities of my application involves moving widgets to new browser windows, allowing users to spread them across multiple scree ...

What is the best way to change the background color for my photo gallery?

I'm currently working on a project to create a unique photo gallery where each image has a different colored background. I have six images in total, but right now all of them have a pink background. I've attempted adding another .popup class in t ...

The specified variable will not be visible in the window object

I recently created a JavaScript code snippet that looks like this: let var1 = 1; window.var2 = 2; After running the code in the Chrome console, I entered window to inspect the global window object. Surprisingly, only the second variable appeared and the ...

Angular.js - index template fails to execute controller, but other templates work flawlessly

I am facing a strange issue with my Angular application that uses ngRoute. I have set up different controllers for each template in the routes.js file: routes.js: angular.module('PokeApp', ['ngRoute']) .config(function($routeProvide ...

Tips for fixing the TS2345 compilation error when working with React

Attempting to implement the setState method in React has resulted in a compile error. Any solutions to this issue would be greatly appreciated. Frontend: react/typescript articleApi.tsx import axios from 'axios'; import {Article} from '../ ...

Following the build in Angular, it only displays the index.html file and a blank screen

According to the information on Angular's official website, running the "ng build" command should generate files in the dist folder ready for hosting. However, after running this command, the index.html file is empty except for the page title. When yo ...

PHP Ajax not updating variable as expected

Apologies for the repetitive questions, but I have tried numerous solutions and cannot seem to figure out why this particular one is not working. I am invoking ajax through a click function, but I am unable to retrieve the jList value and update the variab ...

What could be causing the malfunction in one of the functions within my JavaScript code?

As a JavaScript beginner, I am currently working on creating a To-do App with JavaScript. Most of the functions are functioning perfectly except for one called doneTask at line 36. Despite numerous attempts to identify the issue, I have been unsuccessful s ...

What is the minimum number of lines that can be used for javascript code?

Currently, I am in the process of developing a custom JavaScript minifier. One question that has come up is whether it is necessary to insert line breaks after a certain number of characters on a single line, or if it even makes a difference at all? For i ...

Issue: The specific module is unable to be located, specifically on the Heroku platform

While my application performs well locally and on a Travis CI build server, it encounters issues when deployed on Heroku. The error message Error: Cannot find module is displayed, leading to app crashes. Here are some details about the npm module: It r ...

Boss declares, "Display the iFrame with no borders visible."

Good day to everyone, I am currently dealing with an issue on the page: It seems to be working perfectly in all of my browsers except for Internet Explorer. Since I don't have IE, I'm having trouble troubleshooting this. My boss mentioned somet ...

Tips for swapping out an item mid-scrolling?

What is the best way to change the navbar when scrolling a page in React? How can I achieve this while following React's concepts? Is using getElementById considered bad practice? const useState = React.useState const useEffect = React.useEffect con ...

What causes certain content to be inaccessible when using Safari?

When I visit this page using Safari or my iPhone, the list of social network members does not appear. Can anyone explain why this might be happening? I am unable to figure out the reason behind this issue. Thank you in advance for your help! ...

Transform the character encoding from a non-standard format to UTF-8

Imagine a scenario where there is a page with <meta charset="EUC-KR">, let's call it address-search.foo.com, that allows users to search for an address and sends the data to a specified URL by submitting an HTML form using the POST met ...

Unable to render canvas element

Hey guys, I'm trying to display a red ball on the frame using this function but it's not working. I watched a tutorial and followed the steps exactly, but I can't seem to figure out what's wrong. Can someone please help me with this? I ...

Utilizing AngularJS to handle the reception and storage of HTML entities

I am having an issue storing and displaying HTML content (using an HTML editor) in my AngularJS WebApp. The problem is that the code appears as plain text. Here is the JSApp code: $scope.SkipValidation = function (value) { var decoded = $("#showtext" ...

Executing a script within an ASP.NET MVC Project

Currently, I'm in the process of developing a project in MVC that requires using AJAX to fetch XML from an external source. However, I have encountered a challenge where I am unable to directly make the AJAX call due to a XMLHttpRequest same domain po ...