• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML & CSS Mobile navbar menu

Swìlam

New Coder
im learning hmtl/css and need a menu for my navbar (mobile version ). I watched some java script videos and tried to do same but sadly it dosent work. The menu dosent get hide nothing work tbh. I will be glad if u can advive me or show me a simple way to make one ( my js knowledge is basic ).
Thanks in advance^^

Code:
const mobileBtn = document.getElementById("nav-menu");
nav = document.querySelector("nav");
mobileBtnExit = document.getElementById("nav-exit");

mobileBtn.addEventListener("click", () => {
  nav.classList.add("menu-btn");
});

mobileBtnExit.addEventListener("click", () => {
  nav.classList.remove("menu-btn");
});
/*
===============
NAVBAR
===============
*/
.navbar {
  background: var(--white);
  padding: 10px;
}
.logo {
  font-weight: bold;
  font-size: 1.4em;
  color: var(--black);
}
.logo span {
  color: var(--primary-500);
}
.title {
  display: flex;
  place-content: space-between;
}
#nav-menu {
  margin-top: 5px;
  font-size: 1.4em;
  font-weight: bold;
  cursor: pointer;
  display: block;
}
nav {
  position: fixed;
  z-index: 99;
  width: 66%;
  right: 0;
  top: 0;
  background: var(--primary-600);
  height: 100vh;
  padding: 1em;
}
nav.menu-btn {
  display: block;
}
.home {
  margin-top: 3em;
  margin-bottom: -100px;
}
nav a {
  color: var(--white);
  display: block;
  padding: 0.5em;
  font-size: 1.4em;
  text-align: right;
}
nav a:hover {
  font-weight: bold;
}
#nav-exit {
  display: block;
  float: right;
  margin: 0.5em;
  cursor: pointer;
  font-size: 1.4em;
}
<!-- START Navbar -->
    <section class="navbar">
      <div class="title">
        <a class="logo" href="#">Swilam<span>DE</span></a>
        <span><i id="nav-menu" class="fas fa-bars"></i></span>
      </div>
      <nav>
        <i id="nav-exit" class="fas fa-times"></i>
        <div class="home">
          <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Features</a></li>
            <li><a href="#">Pricing</a></li>
          </ul>
        </div>
        <div class="contact">
          <ul>
            <li><a href="#">Contact</a></li>
            <li><a class="premium" href="#">Go Premium</a></li>
          </ul>
        </div>
      </nav>
    </section>
    <!-- End Navbar -->
 
Last edited by a moderator:

BGB

Active Coder
Staff Team
Guardian
To make your code mobile responsive, you will need a breakpoint.
CSS:
@media screen and (max-width: 480px) {
   /* Your code here */
}
For better understanding please read this article.
 

Top