how to make responsive navbar in javascript

To create a responsive navbar in JavaScript (and HTML/CSS), you can start by first defining the HTML structure of the navbar in your HTML file. Here's an example navbar structure:

<nav class="navbar">
  <a href="#" class="navbar-brand">Logo</a>
  <ul class="navbar-nav">
    <li class="nav-item active">
      <a href="#" class="nav-link">Home</a>
    </li>
    <li class="nav-item">
      <a href="#" class="nav-link">About</a>
    </li>
    <li class="nav-item">
      <a href="#" class="nav-link">Contact</a>
    </li>
    <li class="nav-item">
      <a href="#" class="nav-link">Blog</a>
    </li>
  </ul>
  <div class="navbar-toggler">
    <div class="bar"></div>
    <div class="bar"></div>
    <div class="bar"></div>
  </div>
</nav>
561 chars
23 lines

Next, you can use JavaScript to toggle a "responsive" class on the navbar when the user clicks on the navbar toggler. Here's an example JavaScript code:

index.tsx
const navbarToggler = document.querySelector(".navbar-toggler");
const navbarMenu = document.querySelector(".navbar-nav");

navbarToggler.addEventListener("click", () => {
  navbarToggler.classList.toggle("active");
  navbarMenu.classList.toggle("responsive");
});
265 chars
8 lines

In this code, we first select the navbar toggler and the navbar menu (i.e., the <ul> element that contains the navbar links) using document.querySelector(). Then, we add an event listener to the navbar toggler that toggles the "active" class on the navbar toggler and the "responsive" class on the navbar menu when the toggler is clicked.

Finally, you can use CSS to style the navbar and define the "responsive" class to show/hide the navbar links when the screen size is below a certain threshold. Here's an example CSS code:

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background-color: #f8f9fa;
}

.navbar-brand {
  font-size: 1.5rem;
  font-weight: bold;
}

.navbar-nav {
  display: flex;
  list-style: none;
}

.nav-item {
  margin-right: 1rem;
}

.nav-link {
  color: #212529;
  text-decoration: none;
}

.navbar-toggler {
  display: none;
}

@media (max-width: 768px) {
  .navbar-nav {
    position: absolute;
    top: 100%;
    left: 0;
    display: none;
    flex-direction: column;
    padding: 1rem;
    background-color: #f8f9fa;
    box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
  }

  .navbar-nav.responsive {
    display: flex;
  }

  .nav-item {
    margin-right: 0;
    margin-bottom: 1rem;
  }

  .navbar-toggler {
    display: block;
    cursor: pointer;
  }

  .bar {
    width: 25px;
    height: 3px;
    background-color: #212529;
    margin: 5px auto;
    transition: transform 0.2s ease-in-out;
  }

  .navbar-toggler.active .bar:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
  }

  .navbar-toggler.active .bar:nth-child(2) {
    opacity: 0;
  }

  .navbar-toggler.active .bar:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
  }
}
1215 chars
78 lines

In this code, we define the styles for the navbar (.navbar) and its child elements (.navbar-brand, .navbar-nav, .nav-item, .nav-link, and .navbar-toggler). We also define a media query that applies the "responsive" styles when the screen size is below 768px. In the "responsive" styles, we position the navbar links below the navbar, show/hide them based on the presence of the "responsive" class, and define the styles for the navbar toggler (.navbar-toggler) and its child elements (.bar).

With these HTML, JavaScript, and CSS code snippets, you should be able to create a responsive navbar in your web application.

gistlibby LogSnag