Simple jQuery Drop Down Menu Tutorial

Navigation menu is the crucial role playing item in any web design. Never mistake by ignoring your website navigations because none of design can attract that smartly but a stylish looking navigation menu can do the smart work.

Are you one who looking for some simplest solution of how to make a jQuery drop down menu? If yes! Luckily you’re at the right place. Our this tutorial will help newbies to learn how drop down menus work actually. For the ease of readers we’ll explain the code in parts so let understand the code more conveniently.

This tutorial will be more useful for those with knowledge of the basics of jQuery and CSS. The tutorial doesn’t require that much expertise in coding but at least should have basics know-how so that you don’t comment calling it an alien coding lol. Here’s a quick screenshot of our final menu. Sorry @aliqayyum I’m cloning your website navigation design (smashinghub.com), but you’re so popular that people want things like you have…

Demo and download zip link

For knowing quickly, what we are learning today click on the demo button below to see the result/output of our tutorial. If you’re interested in getting just a copy, click on the download button below to get the copy of jQuery drop down menu.

1- The HTML Markup

While creating menus, the most preferred method is to use some sort of lists which will ease in standardization of the thing. This way styling with CSS become more easier and powerful. For a complete reference of how lists work check this link for reference.

Below is how our HTML markup will look like using the lists.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<ul class="navigation">
<li><a href="#">Main Cat 1</a></li>
<li><a href="#">Main Cat 2 </a>
<ul>
<li><a href="#">Sub Cat 2-1</a></li>
<li><a href="#">Sub Cat 2-2</a></li>
<li><a href="#">Sub Cat 2-3</a></li>
<li><a href="#">Sub Cat 2-4</a></li>
<li><a href="#">Sub Cat 2-5</a></li>
</ul>
<div class="clear"></div>
</li>
<li><a href="#">Main Cat 3 </a>
<ul>
<li><a href="#">Sub Cat 3-1</a></li>
<li><a href="#">Sub Cat 3-2</a></li>
<li><a href="#">Sub Cat 3-3</a></li>
<li><a href="#">Sub Cat 3-4</a></li>
<li><a href="#">Sub Cat 3-5</a></li>
<li><a href="#">Sub Cat 3-6</a></li>
<li><a href="#">Sub Cat 3-7</a></li>
</ul>
<div class="clear"></div>
</li>
<li><a href="#">Main Cat </a></li>
</ul>

2- CSS

Cascade Style Sheets are an important part in creating navigation menus. Without styling it may be near to impossible creating a good looking and stylish menu navigation. We’ll use CSS to show the menus in the horizontal form and to hide the sub menu navigation by default. In this part you can style your menu according to your design.

Lets start with setting the main font-family and the default font size of whole page for better look. This will go like below

1
2
3
4
body{
font-family: Arial, Helvetica,sans-serif;
font-size:15px;
}

After done with font-family setting, let’s adjust the margins, paddings and no list styles to main .navigation class to eliminate the extra spaces and a bullets styling.

1
2
3
4
5
.navigation {
margin:0;
padding:0;
list-style:none;
}

After basic CSS, now let’s trick with positions and code for showing items in-line and we’ll use float:left for the purpose.

1
2
3
4
5
.navigation li {
float:left;
width:150px;
position:relative;
}

The CSS for main category is almost done, now we’ll work on link elements to style them according to our design. We see a lot of people got confuse about why to use display:block.  We set display:block to make sure it covers the whole area available in LI element.

1
2
3
4
5
6
7
8
9
10
.navigation li a {
background:#262626;
color:#fff;
display:block;
padding:8px 7px 8px 7px;
text-decoration:none;
border-top:1px solid #F2861D;
text-align:center;
text-transform:uppercase;
}

and of course on mouse over styling as below

1
2
3
.navigation li a:hover {
color:#F2861D;
}

So now we’re done with the styling of main category menu. Is it really giving nice look? OK fine! We know you’re worried about sub menu styling? Catch them too below.

We’ll put display:none because we don’t want sub menus visible by default So it will be hidden by default. This works like magic, it will hide the sub menu items and jQuery will do the rest of magic. Remaining properties are similar to the main category menu.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
.navigation ul {
position:absolute;
left:0;
display:none;
margin:0 0 0 -1px;
padding:0;
list-style:none;
border-bottom:3px solid #F2861D;
}

.navigation ul li {
width:150px;
float:left;
border-top:none;
}

.navigation ul a {
display:block;
height:15px;
padding:8px 7px 13px 7px;
color:#fff;
text-decoration:none;
border-top:none;
border-bottom:1px dashed #6B6B6B;
}

.navigation ul a:hover {
color:#F2861D;
}

Great, CSS thing is done. You’re going to be a menu pro. The styling thing is over, hopefully you did understood it well and are waiting for the rest jQuery part but before that look at the below combined CSS we’ve done above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Giving a font-family and Size to give good look */
body{
font-family: Arial, Helvetica,sans-serif;
font-size:15px;
}

/* Adjusting the margins, paddings and no list styles */
.navigation {
margin:0;
padding:0;
list-style:none;
}

/* Little tricking with positions */
.navigation li {
float:left; /* Show list items inline */
width:150px;
position:relative;
}

/* Playing with Main Categories */
.navigation li a {
background:#262626;
color:#fff;
display:block; /* Making sure a element covers whole li area */
padding:8px 7px 8px 7px;
text-decoration:none; /* No underline */
border-top:1px solid #F2861D;
text-align:center;
text-transform:uppercase;
}
.navigation li a:hover {
color:#F2861D;
}

/* Sub Cat Menu stuff*/
.navigation ul {
position:absolute;
left:0;
display:none; /* Hide it by default */
margin:0 0 0 -1px;
padding:0;
list-style:none;
border-bottom:3px solid #F2861D;
}

.navigation ul li {
width:150px;
float:left;
border-top:none;
}

/* Sub Cat menu link properties */
.navigation ul a {
display:block; /* Making sure a element covers whole li area */
height:15px;
padding:8px 7px 13px 7px;
color:#fff;
text-decoration:none;
border-top:none;
border-bottom:1px dashed #6B6B6B;
}

.navigation ul a:hover {
color:#F2861D;
}

3- The jQuery

After getting done with HTML markup and Styling, now is the most amazing jQuery part. JQuery did the magic ease in developments, previously achieving JavaScript menu was like climbing a K-2 mountain, a lot of code. We know many people are shaky in jQuery part, so we’ll explain every part written.

We’re going to use .hover() mouse event to trick with mouse enter and mouse leave function and a little animation using fadeIn() and fadeOut() functions. It’s simpler and easier to understand.

Before coding any jQuery, we’ll have to add a jQuery library which is jquery.js in the code to make it work. This can be downloaded at here. Download it and place it in your desired folder, src will be exact location of that file. We’re placing the jquery.js in the same directory.

1
<script type="text/javascript" src="jquery.js"></script>

Let’s begin writing the jQuery now. We’ll first explain the commands separately and then will combine them all at the end.

1
$(document).ready(function () {

The above code makes sure that all the DOM elements are completely loaded on the page. So it only will execute the function once all DOM is fully loaded. See .ready() event reference here.

1
$('.navigation li').hover(

.hover() property will help us set the events for mouse enter and mouse leave for .navigation li. For complete reference of .hover() event, See this link.

1
2
3
4
5
6
7
8
9
function () {
//Fade in the navigation submenu
$('ul', this).fadeIn(); // fadeIn will show the sub cat menu
},
// When mouse leaves the .navigation element
function () {
//Fade out the navigation submenu
$('ul', this).fadeOut(); // fadeOut will hide the sub cat menu
}

Above commenting will be clear and helping. Don’t forget to put <script> tags before writing the jQuery. Combining the whole code will look like below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<script type="text/javascript">
// Executes the function when DOM will be loaded fully
$(document).ready(function () {
// hover property will help us set the events for mouse enter and mouse leave
$('.navigation li').hover(
// When mouse enters the .navigation element
function () {
//Fade in the navigation submenu
$('ul', this).fadeIn(); // fadeIn will show the sub cat menu
},
// When mouse leaves the .navigation element
function () {
//Fade out the navigation submenu
$('ul', this).fadeOut(); // fadeOut will hide the sub cat menu
}
);
});
</script>

The Final Code

Let’s have last look on the final code of HTML, CSS and jQuery combine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<html>
<head>
<title>jQuery Drop Down Menu</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
// Executes the function when DOM will be loaded fully
$(document).ready(function () {
// hover property will help us set the events for mouse enter and mouse leave
$('.navigation li').hover(
// When mouse enters the .navigation element
function () {
//Fade in the navigation submenu
$('ul', this).fadeIn(); // fadeIn will show the sub cat menu
},
// When mouse leaves the .navigation element
function () {
//Fade out the navigation submenu
$('ul', this).fadeOut(); // fadeOut will hide the sub cat menu
}
);
});
</script>

<style type="text/css">

/* Giving a font-family and Size to give good look */
body{
font-family: Arial, Helvetica,sans-serif;
font-size:15px;
}

/* Adjusting the margins, paddings and no list styles */
.navigation {
margin:0;
padding:0;
list-style:none;
}

/* Little tricking with positions */
.navigation li {
float:left; /* Show list items inline */
width:150px;
position:relative;
}

/* Playing with Main Categories */
.navigation li a {
background:#262626;
color:#fff;
display:block; /* Making sure a element covers whole li area */
padding:8px 7px 8px 7px;
text-decoration:none; /* No underline */
border-top:1px solid #F2861D;
text-align:center;
text-transform:uppercase;
}
.navigation li a:hover {
color:#F2861D;
}

/* Sub Cat Menu stuff*/
.navigation ul {
position:absolute;
left:0;
display:none; /* Hide it by default */
margin:0 0 0 -1px;
padding:0;
list-style:none;
border-bottom:3px solid #F2861D;
}

.navigation ul li {
width:150px;
float:left;
border-top:none;
}

/* Sub Cat menu link properties */
.navigation ul a {
display:block; /* Making sure a element covers whole li area */
height:15px;
padding:8px 7px 13px 7px;
color:#fff;
text-decoration:none;
border-top:none;
border-bottom:1px dashed #6B6B6B;
}

.navigation ul a:hover {
color:#F2861D;
}
</style>
</head>
<body>
<ul class="navigation">
<li><a href="#">Main Cat 1</a></li>
<li><a href="#">Main Cat 2 </a>
<ul>
<li><a href="#">Sub Cat 2-1</a></li>
<li><a href="#">Sub Cat 2-2</a></li>
<li><a href="#">Sub Cat 2-3</a></li>
<li><a href="#">Sub Cat 2-4</a></li>
<li><a href="#">Sub Cat 2-5</a></li>
</ul>
<div class="clear"></div>
</li>
<li><a href="#">Main Cat 3 </a>
<ul>
<li><a href="#">Sub Cat 3-1</a></li>
<li><a href="#">Sub Cat 3-2</a></li>
<li><a href="#">Sub Cat 3-3</a></li>
<li><a href="#">Sub Cat 3-4</a></li>
<li><a href="#">Sub Cat 3-5</a></li>
<li><a href="#">Sub Cat 3-6</a></li>
<li><a href="#">Sub Cat 3-7</a></li>
</ul>
<div class="clear"></div>
</li>
<li><a href="#">Main Cat </a></li>
</ul>
<div class="clear"></div>
</body>
</html>

Hopefully you’ve enjoyed this simple jQuery drop down menu. We’ll be waiting for your feedback and comments. Soon we’ll be writing more related code for multi-level drop down menu and some other stuff of jQuery.

Loved the tutorial? Help us spread the word. You can join us @facebook and @twitter.


design

About the author

With a passion for Knowledge, Smashinghub has been created to explore things like Free Resources For Designers, Photographers, Web Developers and Inspiration.

Twitter Visit author website

Subscribe to Smashing Hub


6 Comments

  1. Tim says:

    Is there anyway to replace the hoverOn part of hover with a click? So when you click the main menu the subMenu appears and when you hoverOff the main menu the subMenu disappears?

  2. raman says:

    gr8 article ….bst one for fresher in jquery

  3. jaswanth says:

    Wow really great share, i learned some thing from this article.Thanks much.

  4. rexon says:

    thank you….

  5. WoW Awesome Work thanks sharing this :)

  6. I suggest adding the stop(false,true) function to the jQuery hover state, in order to avoid the bug when a user hovers quickly over various main nav items.

    Awesome beginners’ tutorial, by the way.

    Regards,
    Slavisa