# How to Create a Drop-Down Nav Menu With HTML5, CSS3, and JQuery

In this tutorial, we’ll see what we can achieve with HTML5 and CSS3 when it comes to a staple of current websites: the humble drop-down navigation menu. We’ll also use jQuery to handle the effects and add the finishing touches for us.

HTML5 brings to the spec a dedicated <nav> element that should be used as the container for any major navigation structure, such as the main vertical or horizontal site navigation menus, or an in-page table of contents for example.

Using these new features, we'll build a drop-down navigation menu with clean and semantic HTML code.

Here's what we'll be building:

Let's get started.

## 1. Getting Started

We’ll need a copy of the latest release of jQuery, version 3.6.0 at the time of writing.

Create a project folder somewhere on your machine and call it dropdown-web. Inside this folder, create three new folders: one called index.html for the HTML markup, one called style.css for the styling, and one called script.js for the scripting with jQuery.

## 2. Create the Underlying Page

Begin by importing jQuery and Font Awesome into the HTML head tags:

 1   2   3   4   5   6  Document  7   8   9 10   11  

Next, we code the HTML markup for the nav menu in index.html. You can use any code editor of your choice:

 1   2 
 3 
 4   7   59 
 60 
 61 62   63   64  

Using the HTML5 <header> tag, we create a header containing two sections—the logo section and the navigation menu. The navigation menu contains a list of navigations. Each navigation group has a link, an icon from Font Awesome, and a <div> containing four sub nav links. These nav links will be displayed in the dropdown menu.

We assign class attributes to most of the tags, which will enable us to access them in our stylesheet and jQuery script later on. Finally, below the markup, we use a <script> tag to load our script from the script.js file.

Now let’s add some basic styling. Create the following stylesheet in style.css:

 1 /* reset padding and margin, set document font */  2 * {  3  padding: 0;  4  margin: 0;  5  box-sizing: border-box;  6  font-family: "Roboto", sans-serif;  7 }  8 9 body{  10  width: 100%;  11  height: 100%;  12  background-color: #333;  13 }  14 15 /* Sets color of link texts to white */  16 a{  17  text-decoration: none;  18  color: #fff;  19  font-weight: 700;  20 }  21 22 li {  23  list-style-type: none;  24 }  25 26 /* Header 100 percent of body, position relative to body */  27 header {  28  width: 100%;  29  padding: 0 2%;  30  background-color: black;  31  position: relative;  32 }  33 34 /* Display logo and nav side by side to each other (flex) */  35 .header-container {  36  width: 100%;  37  max-width: 1100px;  38  margin: 0 auto;  39  padding: 10px 0;  40  display: flex;  41  align-items: center;  42  justify-content: space-between;  43 }  44 45 /* Set color for Font Awesome icons */  46 .fas {  47  color: #fff;  48 }  49 50 .logo a {  51  font-size: 40px;  52 }  53 54 .menu {  55  display: flex;  56 }  57 58 .menu li {  59  margin-left: 60px;  60 }  61 62 .menu li a {  63  padding: 20px 0;  64 }  65 66 /* Change color of link texts when hovered upon */  67 .menu li a:hover {  68  color: #fb743e;  69 }  70 71 /* Also change color of icon when hover on link */  72 .menu li a:hover - .fas{  73  color: #fb743e;  74 }  75 76 /* Hide all sub nav links by default, display children in column when displayed */  77 .nav-items-1,.nav-items-2, .nav-items-3, .nav-items-4 {  78  text-align: center;  79  display: none;  80  flex-direction: column;  81  background-color: black;  82 }  83 84 .nav-items-1 a, .nav-items-2 a, .nav-items-3 a, .nav-items-4 a {  85  font-size: 14px;  86 }  87 88 .nav-items-1, .nav-items-2, .nav-items-3 {  89  width: 100%;  90  max-width: 150px;  91  position: absolute;  92  top: 64px;  93  padding: 10px;  94 }  95 96 .nav-items-4 {  97  width: 100%;  98  max-width: 130px;  99  position: absolute;  100  top: 64px;  101 }  102 103 /* This will be used to display sub links for a nav when active */  104 .navActive {  105  display: flex;  106 } 

We began by resetting the padding and margins for all elements to 0px and setting a typeface for our HTML document.

The .navActive class will be toggled onto whichever navigation item the user hovers over. This will in turn toggle the nav link items in the dropdown below the main text.

This feature will be implemented in the following section using jQuery.

The script for toggling each navigation is very simple:

 1 // toggle first nav on hover  2 $('.nav-1').hover(function() {  3 $('.nav-items-1').toggleClass('navActive')  4 })  5 6 // toggle second nav on hover  7 $('.nav-2').hover(function() {  8 $('.nav-items-2').toggleClass('navActive')  9 })  10 11 // toggle third nav on hover  12 $('.nav-3').hover(function() {  13 $('.nav-items-3').toggleClass('navActive')  14 })  15 16 // toggle fourth nav on hover  17 $('.nav-4').hover(function() {  18 $('.nav-items-4').toggleClass('navActive')  19 }) 

In the script, we're saying that whenever a user hovers over any element with a class of nav-1, nav-2, nav-3, or nav-4, we want to display its items inside a dropdown menu by toggling the navActive class on the element.

Recall that in our stylesheet, we hid the dropdown elements by default:

 1 .nav-items-1,.nav-items-2, .nav-items-3, .nav-items-4 {  2  text-align: center;  3  display: none;  4  flex-direction: column;  5  background-color: black;  6 } 

When a user hovers over any of those elements, the jQuery script will toggle this class on the said element:

 1 .navActive {  2  display: flex;  3 } 

As a result, the div element and all of its children will go from hidden display to flex display.

## Final Thoughts

I hope this tutorial helped you understand HTML5 and CSS better. In the process of building this dropdown navigation menu, we covered some basic CSS selectors and a couple of HTML tags to help you structure and design your webpages.