How to create: mobile navigation menu
- Previous Page Super Menu
- Next Page Curtain Menu
Learn how to create a top navigation menu for smartphones/tablets using CSS and JavaScript.
Mobile navigation bar
Vertical (recommended):
Horizontal:
Create a mobile navigation menu
Step 1 - Add HTML:
<!-- Load icon library to display hamburger menu (three bars) on small screens --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <!-- Top Navigation Menu --> <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> </div> <!-- "Hamburger menu" / "Bar icon" to toggle the navigation links --> <a href="javascript:void(0);" class="icon" onclick="myFunction()"> <i class="fa fa-bars"></i> </a> </div>
Second step - Add CSS:
/* Set the style for the navigation menu */ .topnav { overflow: hidden; background-color: #333; position: relative; } /* Hide links within the navigation menu (excluding logo/home page) */ .topnav #myLinks { display: none; } /* Set the style for navigation menu links */ .topnav a { color: white; padding: 14px 16px; text-decoration: none; font-size: 17px; display: block; } /* Set the style of the hamburger menu */ .topnav a.icon { background: black; display: block; position: absolute; right: 0; top: 0; } /* Add gray background color when the mouse hovers */ .topnav a:hover { background-color: #ddd; color: black; } /* Set the style of the active link (or home page/logo) */ .active { background-color: #04AA6D; color: white; }
Third step - Add JavaScript:
/* Toggle the display and hide status of navigation menu links when the user clicks the hamburger menu/horizontal bar icon */ function myFunction() { var x = document.getElementById("myLinks"); if (x.style.display === "block") { x.style.display = "none"; } else { x.style.display = "block"; } }
Related Pages
Tutorial:CSS Navigation Bar
- Previous Page Super Menu
- Next Page Curtain Menu