A Stylish Navigation Menu With jQuery

As you may know, the first 20 seconds of a new visitor’s interaction with a website determine whether they are going to stay or leave. This means you have to follow common design practices and put everything where the user would expect it to be. A crucial part of this process is developing an easy to use navigation menu.

we are going to make a stylish CSS + XHTML navigation menu with the help of the jQuery library.


Step 1 – the XHTML

The XHTML code is simple and search engine friendly.

The entire navigation menu is basically one unordered list. You can introduce new menu items by just adding more LI elements and setting their respective text and target URLs.

Step 2 – jQuery

As you can see from the illustration above, we are going to clone the set of links defined in our XHTML (the dark ones) and assign them the hoverMenu CSS class, which floats them above the default ones.

This strategy has many advantages – firstly it keeps the XHTML cleaner, because you do not have to add those links manually to the page, and secondly it guarantees that you will have clean and working navigation regardless of the visitor’s javascript support – great for both usability and SEO.

Now lets see what happens in our script.js.


// executed after the page has finished loading

$('#navigationMenu li .normalMenu').each(function(){ // for each menu item

// create a duplicate hyperlink and position it above the current one


$('#navigationMenu li').hover(function(){ // using the hover method..

// we assign an action on mouse over
// the animate method provides countless possibilities


// and an action on mouse out



Great, but what have we just done? First we used the $(‘document’).ready method to insure that the code is executed after the page has finished loading.

Then we looped through all the links and cloned them, assigning a new CSS class  – hoverMenu.

After this we used the hover() method to easily add event handlers for both the mouseover and the mouseout event.

Later we used the animate method – a really powerful tool in the jQuery arsenal. In this case we provided it with a new marginTop value and the duration of the effect. jQuery will handle all the animation.

Note the use of the stop() method – it stops all currently active animations and thus prevents the stacking of different animation effects on top of each other guaranteeing a smooth user experience.

Step 3 – the CSS

Lets take a look at our CSS style sheet.

/* Page styles */

body,h1,h2,h3,p,td,quote,small,form,input,ul,li,ol,label{ /* resetting some elements for better browser compatibility */

body{ /* styling the body */
font-family:Arial, Helvetica, sans-serif;

/* Navigation menu styles */

ul{ /* the unordered list */
font-family:Arial, Helvetica, sans-serif;

ul li{
border:1px solid #444444;
display:inline-block; /* changing the display property */
float:left; /* floating the list items to the left */
list-style-type:none; /* disabling the list icon */
overflow:hidden; /* hiding the overflowing content */

ul li a, ul li a:hover,
ul li a:visited{
text-decoration:none; /* removing the underline text-decoration */

/* styling the links */
.normalMenu, .normalMenu:visited,
.hoverMenu, .hoverMenu:visited,
.selectedMenu,.selectedMenu:visited {/* these properties are shared between the classes */
padding:5px 10px;

/* styles that are assigned individually */

.selectedMenu,.selectedMenu:visited {
background:url(img/grey_bg.gif) repeat-x #eeeeee;

.selectedMenu,.selectedMenu:visited {

.normalMenu, .normalMenu:visited{
background:url(img/dark_bg.gif) repeat-x #444444;

As you can see, we have three main classes that define the looks of our navigation menu. normalMenu – for the normal state of the navigation links, hoverMenu – lighter link that slides down on hover, selectedMenu the active (selected) state.


With this our stylish navigation menu is complete!

Be Sociable, Share!

Leave a comment