The icon on my page is not displaying, and the responsive menu is also not appearing

After tweaking the @media {} settings, I noticed that the icon displays properly when reduced, but disappears completely when fully covered with {}. Additionally, my responsive menu is not visible as expected.

`@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
body {
font-family: 'Roboto', sans-serif;
}
*{
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}

nav {
background-color: orange;
height: 80px;
}

nav ul{
float: left;
 margin-left: 10px;
}

nav ul li {
display: inline-block;
line-height: 80px;
margin: 0 15px;
}
nav ul li a {
position: relative;
color:white;
font-size: 18px;
text-transform:uppercase;
padding: 5px 0;
}
nav ul li a:before {
position:absolute;
content: '';
left: 0;
height: 3px;
width: 100%;
bottom: 0;
transform:scaleX(0);
transform-origin: right;
background-color: white;
transition: transform .4s linear;
}
nav ul li a:hover:before{
transform: scaleX(1);
transform-origin: right;
}
label #btn,
label #cancel {
color: white;
font-size: 30px;
float: left;
line-height: 80px;
margin-left: 20px;
cursor:pointer;
display:none;
}
#check {
display:none;
}
@media (max-width: 980px) {
ul {
position:fixed;
height: 100vh;
width: 70%;
background-color: orange;
text-align:left;
top: 80px;
left:-100%;
transition: all .4s;

li {
position:relative;
left: 65px;
}
nav ul li {
display:block;
margin:50px 0;
line-height: 30px;
}
label #btn {
display: block;
}
 nav ul li a {
 font-size: 16px;
 }
}

#check:checked ~ ul {
 left:0;
}
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />


  <nav>
  <input type="checkbox" id="check">
  <label class="check" >
     <i class="fa fa-bars" id="btn" ></i>
     <i class="fa fa-times" id="cancel" ></i>
     </label>
      <ul>
          <li><a href="#" >Home</a></li>
          <li><a href="#" >Forums</a></li>
          <li><a href="#" >Store</a></li>
          <li><a href="#" >Vote</a></li>
          <li><a href="#" >Contact us</a></li>
      </ul>
  </nav>

My objective is for the menu to be displayed upon clicking the icon, however, currently the icon font itself is not showing. Moreover, on larger screens like desktops, the text should be visible while the icon remains hidden, yet this is also not happening at the moment.

Answer №1

Hey, one issue I've spotted is the absence of a closing bracket after your ul tag, which is causing the icon not to display. Additionally, you'll need an onClick function to show the navbar as a hamburger menu.

Just make sure to include the closing tag and the icon will reappear.

Edit:

To make the navbar responsive, try this approach:

In your HTML:

<html lang="en">
 
</head>
<body>

  <nav>
  <input type="checkbox" id="check">
    <a href="javascript:void(0);" class="icon" onclick="myFunction()">
     <i class="fa fa-bars" id="btn"></i>
     </a>
     <i class="fa fa-times" id="cancel" ></i>
     <div id="myLinks">
          <li><a href="#" >Home</a></li>
          <li><a href="#" >Forums</a></li>
          <li><a href="#" >Store</a></li>
          <li><a href="#" >Vote</a></li>
          <li><a href="#" >Contact us</a></li>
         
      </div>
      
  </nav>
</body>
</html>

For CSS adjustments (modify the media query):


@media (max-width: 980px) {
  #myLinks {
    display: none;
  } 

  li {
    position: relative;
    padding: 20px;
    width: 70%;
    background-color: orange;
    text-align: left;
    top: 80px;
    transition: all .4s;
  }

 #btn {
    display: block;
  }

  nav ul li a {
    font-size: 16px;
    color: #ffff;
  }

/* #check:checked + ul {
  left: 0;
} */
}

And for JavaScript functionality:

function myFunction() {
  var x = document.getElementById("myLinks");
  var cancelBtn= document.getElementById("cancel");
  //console.log(x)
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}

You can customize the CSS to suit your needs.

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

We were caught off guard by the TypeScript error: an unexpected token showed up when we were expecting a constructor,

Trying to implement a function within a class in TypeScript. class Test { function add(x: number, y: number): number { return x + y; } } Encountering an error message stating: TypeScript Unexpected token, A constructor, method, access ...

Differences between using tables and divs for form layouts

After searching online for examples of form implementations using DIVs, I noticed that most were simple one-column forms. However, my forms are more complex, like the one shown in this image: Although I can easily create these forms using tables, I encoun ...

Strategies for preserving context throughout an Ajax request

In my project, I am looking to implement an Ajax call that will update a specific child element within the DOM based on the element clicked. Here is an example of the HTML structure: <div class="divClass"> <p class="pClass1">1</p> &l ...

Having trouble pinpointing the issue with my JavaScript code

Recently, I've been experimenting with JavaScript code snippets and after making some edits to one particular sample, I can't seem to figure out why it's not working. Everything appears correct to me, but here is the code snippet (JSFiddle) ...

Which is better for cycling through a list of items: CSS or JavaScript?

Currently, I have a navigation bar set up as an unordered list (<ul>), but some list items are not visible. I want to implement functionality where clicking arrows will move the elements left or right by hiding or showing the leftmost or rightmost it ...

Preserving variable scope in JavaScript even after defining a function

I am facing an issue with my JavaScript code that involves invoking a function within a function: var obj = { // returns the function with prevent default prepended. run: function(functor, context){ return function(e){ e.preventDefault(); ...

How to enhance a class in JavaScript by adding a dynamic property

Within my code, I've defined a class which looks like this: class classA { id: number; name: string; constructor(data){ this.id = data?.id || 0; this.name = data?.name || ''; } } What I aim to do now is add ...

Guide to binding input type= 'email' in Knockout.js

My project utilizes KnockoutJS with MVC. I am seeking assistance on determining whether an emailId is valid or invalid. Based on this validation, I need to dynamically enable/disable a button and set an error title for the corresponding textbox. Below is ...

Access the document on the desktop and open it in a new popup window

As a novice in html coding, I'm wondering if it's possible to write window size and other properties directly into the page. Let me explain. I'm currently working on a calculator and I want to run the HTML file on my desktop. Everything wor ...

The latest version of Material UI, v4, does not currently support React 18

Looking to incorporate MUI (Material UI) into my website design. Encountering difficulties with installing this library, receiving the error message below: -npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While ...

It is impossible for Javascript to access an input element within a gridview

I have developed an asp.net page that allows a site administrator to select a user as the 'systems chair'. The page displays users in a gridview and includes a column of radio buttons to indicate who the current chair is or to change the assigned ...

Resetting the internal state in Material UI React Autocomplete: A step-by-step guide

My objective is to refresh the internal state of Autocomplete, a component in Material-UI. My custom component gets rendered N number of times in each cycle. {branches.map((branch, index) => { return ( <BranchSetting key={ind ...

Using Jquery to run a jquery script stored in a variable within jQuery syntax

This question may seem a bit confusing, so allow me to explain further. I am trying to execute a jquery script that is written in a text file obtained through an ajax request. For example, the code I receive from the ajax request looks like this: ($($(" ...

Executing a jQuery AJAX request for a second time

Upon hitting the submit button for the first time, the codes work successfully. However, upon hitting the button for the second time with correct email and password values, nothing happens and the user cannot log in. I have identified that the issue lies w ...

Highcharts displaying black color after AJAX refresh

Struggling to implement real-time data visualization using Highcharts, I found myself tired of sifting through documentation and feeling confused. My solution involves using AJAX refresh. When the page initially reloads, my chart renders properly. However, ...

How can you eliminate the first elements of two or more arrays of objects until all of their first elements match based on a specific field?

My Typescript code includes a Map object called `stat_map` defined as const stat_map: Map<string, IMonthlyStat[]> = new Map(); The interface IMonthlyStat is structured as shown below (Note that there are more fields in reality) export interface IMon ...

Tips for correctly positioning CSS elements:

I am currently working on a slider using noUi Slider and aiming for an elegant solution. To accommodate the large size of the handle, I expanded the base UI with extra values which are not allowed, causing the handle to jump back to the permitted values. T ...

Encountering an ETIMEDOUT error while sending out large (10k) post requests with axios.all in a Node

I have implemented axios.all to make simultaneous post calls. Below is the code snippet: let postUrls = []; data.forEach(item => { const itemData = { stream: stream_name, key: item.serialNumber, addr ...

What is the best way to showcase nested array JSON data in an HTML Table?

https://i.stack.imgur.com/OHL0A.png I attempted to access the following link http://jsfiddle.net/jlspake/v2L1ny8r/7/ but without any success. This is my TypeScript code: var viewModel = function(data){ var self = this; self.orders = ko.observableArr ...

What is the reason that styled() does not apply styles to the Material-UI <TextField /> component?

I've managed to figure out a solution, but I'm curious about why this code is working: <TextField label='Zip Code' size='small' InputProps={{ style: { borderRadius: '.4rem' }, }} sx={{ width: & ...