How do you create a responsive navigation menu using CSS?

How do you create a responsive navigation menu using CSS?

To create a responsive navigation menu using CSS, you can use media queries to adjust the layout and appearance of the menu depending on the screen size.

Here is a sample code that demonstrates how to create a basic responsive navigation menu using CSS:

HTML code:

<nav>

  <ul>

    <li><a href="#">Home</a></li>

    <li><a href="#">About Us</a></li>

    <li><a href="#">Services</a></li>

    <li><a href="#">Contact Us</a></li>

  </ul>

</nav>


CSS code:

/* Navigation menu styles */

nav {

  background-color: #333;

  color: #fff;

  display: flex;

  justify-content: space-between;

  align-items: center;

  padding: 10px;

}


nav ul {

  list-style: none;

  margin: 0;

  padding: 0;

  display: flex;

}


nav ul li {

  margin: 0 10px;

}


nav ul li a {

  color: #fff;

  text-decoration: none;

}


/* Media query for smaller screens */

@media (max-width: 768px) {

  nav {

    flex-direction: column;

    align-items: flex-start;

  }


  nav ul {

    flex-direction: column;

    align-items: flex-start;

    display: none;

  }


  nav ul.show {

    display: flex;

  }


  nav ul li {

    margin: 10px 0;

  }


  nav ul li a {

    display: block;

    padding: 10px;

    border-bottom: 1px solid #fff;

  }


  nav .toggle {

    display: block;

    color: #fff;

    font-size: 20px;

    cursor: pointer;

    margin-left: auto;

  }

}

In the CSS code above, we first style the navigation menu for larger screens using nav, nav ul, and nav ul li selectors. Then, we use a media query to adjust the layout for smaller screens using the @media rule with a max-width of 768px. In this example, we have chosen 768px as the breakpoint for the mobile layout, but you can adjust this value as needed.

Inside the media query, we modify the nav and nav ul selectors to change the layout to a column and hide the list items respectively. We also add a new class called .show to the nav ul selector, which we will use later to show the list items when the user clicks on the toggle button.

We then style the list items and links for the mobile layout, and add a toggle button with a class of .toggle.

To show the list items when the user clicks on the toggle button, we add some JavaScript code:

<script>

  const toggleButton = document.querySelector('.toggle');

  const navList = document.querySelector('nav ul');


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

    navList.classList.toggle('show');

  });

</script>

In this code, we first select the toggle button and the navigation list using document.querySelector. We then add an event listener to the toggle button that toggles the .show class on the navigation list when the user clicks on the toggle button.

With this code, the navigation menu should be responsive and work well on both desktop and mobile devices.

If you enjoy this article or find it helpful. Please like, comment, and share this post.

Comments

Popular posts from this blog

What is $wpdb in WordPress?

What is FTP ? Which is best FTP Protocol How we can use It ?