Home > Net >  Problem With Navbar Elements Using Basics CSS
Problem With Navbar Elements Using Basics CSS

Time:06-17

I've just started out with the basics of CSS and HTML, there's this problem that I am facing, In my Navbar , when I click on the Hamburger button , The properties that I had added to my text get reverted back to the default one , like the text-decoration changes to underline , How Do I Fix this?

HTML code

<html lang="en">

<head>
  <title>My CSS Website</title>
  <link href="styles.css" rel="stylesheet">
</head>

<body>
  <nav>
    <ul  id="dropdownMenu">
      <li><a href="#home">Home</a></li>
      <li><a href="#news">News</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><a href="#about">About</a></li>
      <li ><a href="#signup">Sign Up</a></li>
      <li ><a href="#signin">Sign in</a></li>
      <li ><a href="javascript:void(0)" onclick="Hamburger()">&#9776;</li>
    </ul>
  </nav>
  
  <script>
    function Hamburger(){
      var x=document.getElementById("dropdownMenu");
      if(x.className==="top-nav"){
        x.className ="responsive";
      }
      else{
        x.className="top-nav";
      }
    }
  </script>
  
</body>

</html>

CSS


*{
    margin:0;
    padding:0;
}

nav,header,footer{
    display: block;
}

body{
    line-height: 1.5;
}
/* Navbar */
nav{
    width:100%;
}
nav ul{
    background-color: #eee;
    overflow:hidden;
} 
ul.top-nav li{
    list-style:none;
    float: left;
}
ul.top-nav li.right-nav{
    min-height: 16px;
    float: right;

}
ul.top-nav li a{
    text-decoration: none;
    padding: 1.25rem;
    min-height:16px;
    text-align: center;
    text-transform: uppercase;
    color:rgb(54, 49, 49);
    display:block;
}
ul.top-nav li a:hover{
    background-color: #0080ff;
    color: #fff;
}
ul.top-nav li.dropdownIcon{
    display: none;
}

/* Mobile Version */

@media screen and (max-width:680px){
    ul.top-nav li:not(:nth-child(1)){
        display:none;
    }
    ul.top-nav li.dropdownIcon{
        display: block;
        float:right;
    }
    ul.top-nav.responsive{
        position:relative;
    }
    ul.top-nav.responsive li{
        display: inline;
        float:none;
    }
    ul.top-nav.responsive li a {
        display:block;
        text-align:left;
        text-decoration: none;
    }
} ```

CodePudding user response:

Because you don't add class in the right way also change onclick, don't wrap function into the string "Hamburger()" use this Hamburger()

 <li ><a href="javascript:void(0)" onclick=Hamburger()>&#9776;</li>
</ul>

*{
    margin:0;
    padding:0;
}

nav,header,footer{
    display: block;
}

body{
    line-height: 1.5;
}
/* Navbar */
nav{
    width:100%;
}
nav ul{
    background-color: #eee;
    overflow:hidden;
} 
ul.top-nav li{
    list-style:none;
    float: left;
}
ul.top-nav li.right-nav{
    min-height: 16px;
    float: right;

}
ul.top-nav li a{
    text-decoration: none;
    padding: 1.25rem;
    min-height:16px;
    text-align: center;
    text-transform: uppercase;
    color:rgb(54, 49, 49);
    display:block;
}
ul.top-nav li a:hover{
    background-color: #0080ff;
    color: #fff;
}
ul.top-nav li.dropdownIcon{
    display: none;
}

/* Mobile Version */

@media screen and (max-width:680px){
    ul.top-nav li:not(:nth-child(1)){
        display:none;
    }
    ul.top-nav li.dropdownIcon{
        display: block;
        float:right;
    }
    ul.top-nav.responsive{
        position:relative;
    }
    ul.top-nav.responsive li{
        display: inline;
        float:none;
    }
    ul.top-nav.responsive li a {
        display:block;
        text-align:left !important;
        text-decoration: none !important;
    }
}
  <nav>
    <ul  id="dropdownMenu">
      <li><a href="#home">Home</a></li>
      <li><a href="#news">News</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><a href="#about">About</a></li>
      <li ><a href="#signup">Sign Up</a></li>
      <li ><a href="#signin">Sign in</a></li>
      <li ><a href="javascript:void(0)" onclick={Hamburger()}>&#9776;</li>
    </ul>
  </nav>
<script>
     function Hamburger(){
      var x=document.getElementById("dropdownMenu");
      if(x.className==="top-nav"){
        x.classList.add("responsive")
        // x.className =" " "responsive";
      }
      else{
        x.className="top-nav";
      }
    }
</script>

  function Hamburger(){
  var x=document.getElementById("dropdownMenu");
  if(x.className==="top-nav"){
   //add class like this
    x.className =" " "responsive";
          or
   x.classList.add("responsive");
  }
  else{
    x.className="top-nav";
  }
}

CodePudding user response:

JavaScript Class

  • if you want get class list then use classList and classList return list

    const list = element.classList
    
  • if you want to add class then.

    element.classList.add("class_name"); 
    
  • if you want to remove class then.

    element.classList.remove("class_name");
    
  • if you want to toggle class then. (toggle check if class exist then remove class and else add class).

    element.classList.toggle("class_name");
    

*{
    margin:0;
    padding:0;
}

nav,header,footer{
    display: block;
}

body{
    line-height: 1.5;
}
/* Navbar */
nav{
    width:100%;
}
nav ul{
    background-color: #eee;
    overflow:hidden;
} 
ul.top-nav li{
    list-style:none;
    float: left;
}
ul.top-nav li.right-nav{
    min-height: 16px;
    float: right;

}
ul.top-nav li a{
    text-decoration: none;
    padding: 1.25rem;
    min-height:16px;
    text-align: center;
    text-transform: uppercase;
    color:rgb(54, 49, 49);
    display:block;
}
ul.top-nav li a:hover{
    background-color: #0080ff;
    color: #fff;
}
ul.top-nav li a:active{
    background-color: #0080ff;
    color: #fff;
}
ul.top-nav li.dropdownIcon{
    display: none;
}

/* Mobile Version */

@media screen and (max-width:680px){
    ul.top-nav li:not(:nth-child(1)){
        display:none;
    }
    ul.top-nav li.dropdownIcon{
        display: block;
        float:right;
    }
    ul.top-nav.responsive{
        position:relative;
    }
    ul.top-nav.responsive li{
        display: inline;
        float:none;
    }
    ul.top-nav.responsive li a {
        display:block;
        text-align:left;
        text-decoration: none;
    }
} 
<head>
  <title>My CSS Website</title>
  <link href="styles.css" rel="stylesheet">
</head>

<body>
  <nav>
    <ul  id="dropdownMenu">
      <li><a href="#home">Home</a></li>
      <li><a href="#news">News</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><a href="#about">About</a></li>
      <li ><a href="#signup">Sign Up</a></li>
      <li ><a href="#signin">Sign in</a></li>
      <li ><a href="javascript:void(0)" onclick="Hamburger()">&#9776;</li>
    </ul>
  </nav>
  
  <script>
    function Hamburger(){
      var x=document.getElementById("dropdownMenu");
      x.classList.toggle("responsive");
    }
  </script>
  
</body>

I think this code is help you! thank you!

  • Related