Techumber
Home Blog Work

How to Create a Responsive Navigation Bar with HTML, CSS, and JavaScript

Published on August 9, 2023

Introduction

In this post, we will create a responsive navigation bar using HTML, CSS, and JavaScript. We will start by creating the basic structure of the navigation bar and then add styles to make it look good on different devices. Finally, we will add some interactivity to the navigation bar so that it collapses or expands when the user clicks on the button.

Creating the Navigation Bar Structure

To create a responsive navigation bar, we first need to create the basic structure of the navigation bar using HTML and CSS. Here is an example of what the HTML code for the navigation bar might look like:

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

This HTML code creates a navigation bar with three links: Home, About, and Contact. We will add styles to this navigation bar in the next section.

Styling the Navigation Bar

To make the navigation bar responsive, we need to add some CSS styles to it. Here is an example of what the CSS code for the navigation bar might look like:

nav {
  background-color: #333;
  padding: 10px;
}

nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: space-between;
}

nav li {
  margin: 0 20px;
}

nav a {
  color: #fff;
  text-decoration: none;
}

nav a:hover {
  background-color: #444;
}

This CSS code sets the background color of the navigation bar to #333, adds some padding around the links, and makes the links display as a flexbox. It also styles the links with white text on a dark background and adds a hover effect that changes the background color when the user hovers over it.

Adding Interactivity to the Navigation Bar

To make the navigation bar responsive, we need to add some JavaScript code to it. Here is an example of what the JavaScript code for the navigation bar might look like:

const nav = document.querySelector('nav');
const button = document.createElement('button');
button.textContent = 'Toggle Menu';
button.addEventListener('click', () => {
  if (nav.classList.contains('menu-open')) {
    nav.classList.remove('menu-open');
  } else {
    nav.classList.add('menu-open');
  }
});

This JavaScript code creates a button element and adds it to the navigation bar. When the user clicks on the button, the addEventListener function is called and it checks if the menu-open class is present on the navigation bar. If it is, it removes the class. If it is not, it adds the class. This causes the navigation bar to collapse or expand when the user clicks on the button.

Conclusion

In this post, we created a responsive navigation bar using HTML, CSS, and JavaScript. We started by creating the basic structure of the navigation bar and then added styles to make it look good on different devices. Finally, we added some interactivity to the navigation bar so that it collapses or expands when the user clicks on the button.

I hope this tutorial has been helpful in creating a responsive navigation bar for your website. Let me know if you have any questions or if you would like more information on how to create a responsive navigation bar using HTML, CSS, and JavaScript.