22 Apr 2023

Creating a responsive navigation menu with HTML, CSS, and JavaScript

A navigation menu is an essential component of any website that helps users navigate through the website's content. With the rise of mobile devices, it is crucial to make the navigation menu responsive and adaptable to different screen sizes. In this tutorial, we will show you how to create a responsive navigation menu using HTML, CSS, and JavaScript.

HTML Structure

The first step in creating a navigation menu is to set up the HTML structure. We will create an unordered list to represent the menu items, with each list item containing a link to the relevant page. Here is an example of the HTML structure for our navigation menu:

<nav class="navbar">
  <ul class="nav-links">
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Contact</a></li>

CSS Styling

Now that we have set up the HTML structure, we will add some CSS styles to create a basic navigation menu. We will use flexbox to align the menu items horizontally and set the background color and font styles. Here is the CSS code for the navigation menu:

.navbar {
  background-color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 50px;

.nav-links {
  display: flex;
  list-style: none;

.nav-links li {
  margin-right: 20px;

.nav-links a {
  color: #fff;
  text-decoration: none;
  font-size: 18px;
  font-weight: bold;

JavaScript for Responsive Navigation

Now that we have created a basic navigation menu, we will add some JavaScript code to make it responsive. When the screen size is small, we want to hide the menu items and show a toggle button that can be clicked to show the menu. Here is the JavaScript code for our responsive navigation menu:

const toggleButton = document.getElementsByClassName('toggle-button')[0];
const navbarLinks = document.getElementsByClassName('nav-links')[0];

toggleButton.addEventListener('click', () => {

We first select the toggle button and the menu items using their class names. We then add an event listener to the toggle button that toggles the "active" class on the menu items when clicked. We will add the "active" class to the CSS code to show the menu items when the toggle button is clicked.

@media screen and (max-width: 768px) {
  .nav-links {
    position: fixed;
    height: 100vh;
    width: 100%;
    background-color: #333;
    top: 80px;
    left: -100%;
    transition: all 0.5s ease;
  .nav-links.active {
    left: 0%;
  .toggle-button {
    position: absolute;
    top: 20px;
    right: 20px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 24px;
    cursor: pointer;
  .toggle-button .bar {
    height: 3px;
    width: 100%;
    background-color: #fff;
    border-radius: 10px;

We use media queries to add styles for smaller screen sizes. We set the position of the menu items to fixed and set the height and width to 100% to cover the entire screen. We also set the left property to - 100% to hide the menu items off-screen. We then add a transition effect to animate the menu when the toggle button is clicked.

We also add styles for the toggle button, setting its position to absolute and adding three bars to represent the button. We use flexbox to align the bars and set their background color and height.


In this tutorial, we have shown you how to create a responsive navigation menu using HTML, CSS, and JavaScript. We started by creating the HTML structure for the menu items and added some basic CSS styles to make it look good. We then added some JavaScript code to make the menu responsive and adaptable to different screen sizes. With this navigation menu, your website will be easy to navigate on both desktop and mobile devices.