1. Code
  2. JavaScript
  3. jQuery

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

Scroll to top

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
<html lang="en">
2
<head>
3
  <meta charset="UTF-8">
4
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
5
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
6
  <title>Document</title>
7
  <link rel="stylesheet" href="style.css">
8
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/k7ujTnHg4CGR2D7kSs0v4LLanw2qksYuRlEzO+tcaEPQogQ0KaoGN26/zrn20ImR1DfuLWnOo7aBA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
9
10
  <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
11
</head>

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

1
<body>
2
  <header>
3
    <div class="header-container">
4
      <div class="logo">
5
        <a href="">LOGO</a>
6
      </div>
7
      <nav>
8
        <ul class="menu">
9
10
          <!-- Nav -->
11
          <li class="nav-1">
12
            <a href="">Nav Link 1</a>
13
            <i class="fas fa-arrow-down"></i>
14
            <div class="nav-items-1">
15
              <a href="">Sub Nav Link 1</a>
16
              <a href="">Sub Nav Link 2</a>
17
              <a href="">Sub Nav Link 3</a>
18
              <a href="">Sub Nav Link 4</a>
19
            </div>
20
          </li>
21
22
          <!-- Nav -->
23
          <li class="nav-2">
24
            <a href="">Nav Link 2</a>
25
            <i class="fas fa-arrow-down"></i>
26
            <div class="nav-items-2">
27
              <a href="">Sub Nav Link 1</a>
28
              <a href="">Sub Nav Link 2</a>
29
              <a href="">Sub Nav Link 3</a>
30
              <a href="">Sub Nav Link 4</a>
31
            </div>
32
          </li>
33
34
          <!-- Nav -->
35
          <li class="nav-3">
36
            <a href="">Nav Link 3</a>
37
            <i class="fas fa-arrow-down"></i>
38
            <div class="nav-items-3">
39
              <a href="">Sub Nav Link 1</a>
40
              <a href="">Sub Nav Link 2</a>
41
              <a href="">Sub Nav Link 3</a>
42
              <a href="">Sub Nav Link 4</a>
43
            </div>
44
          </li>
45
46
          <!-- Nav -->
47
          <li class="nav-4">
48
            <a href="">Nav Link 4</a>
49
            <i class="fas fa-arrow-down jsicon-2"></i>
50
            <div class="nav-items-4">
51
              <a href="">Sub Nav Link 1</a>
52
              <a href="">Sub Nav Link 2</a>
53
              <a href="">Sub Nav Link 3</a>
54
              <a href="">Sub Nav Link 4</a>
55
            </div>
56
          </li>
57
        </ul>
58
      </nav>
59
    </div>
60
  </header>
61
62
  <script src="script.js"></script>
63
</body>
64
</html>

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.

3. Add CSS

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.

4. Add the Script

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.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.