How To Design Mobile Navigation Menu Using Javascript

In this blog, we will learn how to use CSS and JavaScript to create a top navigation menu for smartphones and tablets and understand the mobile menus with dynamic design.

What Is Mobile Responsive Design?

->  Mobile responsiveness refers to a website’s capacity to be functional and aesthetically pleasing across devices of varying sizes.

Use this class name here. myLinks specifies which menus should be displayed in the menu-list.


<div class="topnav">
  <a href="#home" class="active">Logo</a>
  <!-- Navigation links (hidden by default) -->
  <div id="myLinks">
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
    <a href="#about">About</a>
  <!-- "Hamburger menu" / "Bar icon" to toggle the navigation links -->
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
    <i class="fa fa-bars"></i>

The following file includes the styles for a mobile navigation menu.


/* Style the navigation menu */
.topnav {
  overflow: hidden;
  background-color: #333;
  position: relative;

/* Hide the links inside the navigation menu (except for logo/home) */
.topnav #myLinks {
  display: none;

/* Style navigation menu links */
.topnav a {
  color: white;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
  display: block;

/* Style the hamburger menu */
.topnav a.icon {
  background: black;
  display: block;
  position: absolute;
  right: 0;
  top: 0;

/* Add a grey background color on mouse-over */
.topnav a:hover {
  background-color: #ddd;
  color: black;

/* Style the active link (or home/logo) */
.active {
  background-color: #04AA6D;
  color: white;

In the JavaScript file,

Create an event for a button click. On click of this button, open a menu and display a menu list, and when that button is again clicked that will shut a menu list.


function myFunction() {
  var x = document.getElementById("myLinks");
  if ( === "block") { = "none";
  } else { = "block";

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories