DEV Community

Cover image for How to Build a Responsive Navigation Bar using CSS and JavaScript
Satyanchal
Satyanchal

Posted on

 

How to Build a Responsive Navigation Bar using CSS and JavaScript

How to Build a Responsive Navigation Bar using CSS and JavaScript

A navigation bar is one of the most important components of any website. Not only does it provide a roadmap for your website visitors, but it also helps in improving the user experience. Thus, creating a responsive navigation bar that looks good on all devices is extremely important. In this post, I’ll guide you on how to build a responsive navigation bar using CSS and JavaScript.

Step 1: HTML Markup

First, we start by creating the HTML markup for our navigation bar. It is a standard <nav> element that contains an unordered list with a few list items that will serve as menu items. Here is the code:

<nav class="navbar">
  <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>
Enter fullscreen mode Exit fullscreen mode

Step 2: Styling

Next, we move on to styling the navigation bar using CSS. We’ll use flexbox to create a horizontal navigation bar with each menu item evenly spaced out. We’ll also add a background color, and a hover effect on the menu items.

.navbar {
  background-color: #333;
}

.menu {
  display: flex;
  justify-content: space-evenly;
  list-style: none;
  margin: 0;
  padding: 0;
}

.menu li {
  margin: 5px;
}

.menu li a {
  color: #fff;
  text-decoration: none;
  transition: all 0.3s ease;
}

.menu li a:hover {
  color: #d6d6d6;
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Mobile Menu

Now we need to make our navigation bar responsive for smaller devices. One way to do this is by hiding the menu on smaller devices and displaying a button that shows the menu when clicked. Here is the code:

<nav class="navbar">
  <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
  <button class="menu-btn">&#9776;</button>
</nav>
Enter fullscreen mode Exit fullscreen mode
.menu-btn {
  background-color: #333;
  border: none;
  color: #fff;
  cursor: pointer;
  display: none;
  font-size: 25px;
  margin: 0;
  padding: 10px 15px;
  text-align: center;
}

@media screen and (max-width: 600px) {
  .menu {
    display: none;
    flex-direction: column;
    margin-top: 50px;
  }
  .menu li {
    margin: 0;
    text-align: center;
    width: 100%;
  }
  .menu-btn {
    display: block;
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: JavaScript

Finally, we’ll use JavaScript to show and hide the menu when the button is clicked. Here is the code:

const menuBtn = document.querySelector(".menu-btn");
const menu = document.querySelector(".menu");

let showMenu = false;

menuBtn.addEventListener("click", toggleMenu);

function toggleMenu() {
  if (!showMenu) {
    menu.classList.add("show");
    showMenu = true;
  } else {
    menu.classList.remove("show");
    showMenu = false;
  }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this tutorial, we learned how to build a simple responsive navigation bar using CSS and JavaScript. You can customize the navigation bar according to your needs and add more features as required. Remember, a navigation bar is an important element of your website, and a responsive one can really make a positive impact on your user experience.

Top comments (5)

Collapse
 
artydev profile image
artydev •

Nice thank you :-)

Here is an alternative to add and remove classes.

<button class="trigger"> Menu</button>

<ul class="menu" data-state="opened">
  <li>TEST</li>
  <li>TEST</li>
  <li>TEST</li>
  <li>TEST</li>   
</ul>

<script>
  let qs = (selector) => document.querySelector(selector)

  let btn =  qs(".trigger")
  let menu = qs(".menu")

  btn.addEventListener("click", () => {
    let menustate  = menu.dataset.state
    menu.dataset.state = 
      menustate == "opened" ? "closed" : "opened"
  })
</script>
Enter fullscreen mode Exit fullscreen mode
.menu[data-state="opened"] {
  display:block
}

.menu[data-state="closed"] {
  display:none
}
Enter fullscreen mode Exit fullscreen mode

You can test it here : ToggleMenu

Regards

Collapse
 
gilfewster profile image
Gil Fewster •

I recommend using a checkbox unlit rather than a button, and then you don’t need any JS at all — just use the checked attribute on the checkbox.

Collapse
 
artydev profile image
artydev • • Edited

Thank you.
In this simple case, you are right.
But in case of multiple conditions, data-attributes are great:

.menu[data-state="opened"] {
  display:block
}

.menu[data-sealed="true"] {
  background: red;
}

.menu[data-sealed="false"] {
  background: green
}


.menu[data-state="closed"] {
  display:none
}
Enter fullscreen mode Exit fullscreen mode
<button class="trigger"> Menu</button>

<button class="locker"> Locker</button>
<button class="unlocker"> UnLocker</button>

<ul class="menu" data-state="closed" data-sealed="false">
  <li>TEST</li>
  <li>TEST</li>
  <li>TEST</li>
  <li>TEST</li>   
</ul>

<script>
  let qs = (selector) => document.querySelector(selector)

  let btn =  qs(".trigger")
  let lock = qs(".locker")
  let unlock = qs(".unlocker")
  let menu = qs(".menu")

  lock.disabled = ""
  unlock.disabled = "disabled"

  function Lock (locked = false) {
    if (!locked) {
      lock.disabled = "" 
      unlock.disabled = "disabled"
    }
    else {
      lock.disabled = "disabled" 
      unlock.disabled = "" 
    }
  }

  lock.addEventListener("click",  () => {
    menu.dataset.sealed = "true";
    Lock(true)
  })

  unlock.addEventListener("click",  () => {
    menu.dataset.sealed = "false"
    Lock(false)
  })

  btn.addEventListener("click", () => {
    let menustate  = menu.dataset.state
    if (menu.dataset.sealed == "false") {
       menu.dataset.state =  menustate == "opened" ? "closed" : "opened"
    }
  })
</script>
Enter fullscreen mode Exit fullscreen mode

You can test it here : MultCond

Thread Thread
 
gilfewster profile image
Gil Fewster •

Yeah, I’m definitely with you there. I’m a big fan of data attributes.

Collapse
 
corners2wall profile image
Corners to wall •

How about minmax function?