Advertisement

How to Build a Kick-Butt CSS3 Mega Drop-Down Menu

by
Gift

Start a hosting plan from $3.92/mo and get a free year on Tuts+ (normally $180)

This post is part of a series called CSS3 Mastery.
Build Awesome Practical CSS3 Buttons

Often used on e-commerce or large scale websites, mega menus are becoming more and more popular, as they offer an effective solution to displaying a lot of content while keeping a clean layout. In this tutorial, we'll learn how to build a cross-browser, awesome CSS-only drop-down mega menu, using nice CSS3 features.


Step 1: Building the Navigation Bar

Let's begin with a basic menu, built with an unordered list and some basic CSS styling.

Creating the Menu Container

We'll now apply some basic CSS styling. For the menu container, we define a fixed width that we center by setting the left and right margins to "auto".

Now, let's see how we can improve it with some CSS3 features. We need to use different syntaxes for Webkit-based browsers (like Safari) and for Mozilla-based browsers (like Firefox).

For rounded corners, the syntax will be :

For the background, we'll use gradients and a fallback color for older browsers. To keep consistency when choosing colors, there is an awesome tool called Facade that helps you find lighter and darker tones of a basic color.

The first line applies a simple background color (for older browsers); the second and third lines create a gradient from the top to the bottom using two colors : #0272a7 and #013953.

We can now add a darker border and polish the design with a "fake" inset border created with the "box-shadow" feature. The syntax is the same for all compatible browsers: the first value is the horizontal offset, the second one is the vertical offset, the third one is the blur radius (a small value makes it sharper; it will be 1 pixel in our example). We set all offsets to 0 so the blur value will create a uniform light border :

Here's the final CSS code for the #menu container :

Styling Menu Items

We will begin with all menu items aligned to the left and space them with a margin-right (the padding will be necessary for the hover state) :

For the hover state and the drop down, I have chosen a grey color scheme for the background.

The fallback color will be a light grey (#F4F4F4) and the gradient will be applied from the top (#F4F4F4) to the bottom (#EEEEEE). Rounded corners will be applied only on top corners as we'll have the drop down sticking right under the menu items.

The left and right padding is slightly smaller here because we have a border of 1 pixel appearing on hover. If we keep the same padding, menu items will be pushed two pixels on the right because of the left and right borders added on mouse hover. To avoid that, we'll remove 1 pixel of padding on both sides, so we have 9 pixels instead of 10.

Then, we add rounded corners to the top only so the drop down will stick perfectly under the parent menu item :

Here is the final CSS for the menu items on hover :

For the links, we'll apply a nice text shadow using a simple syntax : the first and second values are horizontal and vertical offsets for the shadow (1 pixel in our example), the third one is the blur (1 pixel too) and then we have the (black) color :

Here is the final CSS for the links :

On mouse hover, as the background is grey, we'll use a darker color (#161616) for the links and the white color for the text shadow :

Finally, we need a way to indicate if there's a drop down or not by using a simple arrow image as background, it will be positioned on the right using padding and the top margin will align to it properly. On hover this top margin will be set to 7 pixels instead of 8 as we have an additional border appearing on mouse hover (otherwise, the arrow would be pushed 1 pixel down on hover) :

Here is our final code for the menu container and links; only the "home" item doesn't have any drop down content for now :

And the result is :

Building a CSS3 Mega Drop Down Menu

Step 2: Coding the Drop Down

A "classic" drop down menu usually contains lists nested within parent list items and looks like:

General Structure

For our Mega Menu, instead of nested lists, we'll simply use standard DIVs, which will work like any nested list :

This will be the basic structure for the drop down. The idea behind it is to be able to include any kind of content, such as paragraphs, images, custom lists or a contact form, organized into columns.

Drop Down Containers

Containers with different sizes will hold the entire drop down content. I've chosen the tag names according to the number of columns they will contain.

To hide the drop downs, we'll use absolute positioning with a negative left margin :

The background fallback color is the same as the one used for the menu items. Modern browsers will display a gradient starting with #EEEEEE at the top (to match the parent menu item gradient) and ending with #BBBBBB at the bottom:

We'll again use rounded corners, except for the top left one :

To illustrate this, let's see how it would look if we hadn't paid attention to detail:

Building a CSS3 Mega Drop Down Menu

Now here is our example:

Building a CSS3 Mega Drop Down Menu

As you can see, the drop down sticks nicely to its parent menu item.

In order to have a perfect drop down container, we need to specify the width for each one :

And to show the drop downs on mouse hover, we'll simply use :

Using the Drop Down Containers

Our classes are ready to be included in our menu. We'll use each one of them starting from the 5-column, layout to the single column drop down :

Here is a preview of the code above :

Building a CSS3 Mega Drop Down Menu

Step 3: Creating the Drop Down Container Columns

Now that we have the containers ready, we'll create columns of increasing sizes, following the principles of the 960 grid system.

Using Columns

Here is an example of a drop down containing several columns. In this example, we have different combinations using all kinds of columns :

Code preview :

Building a CSS3 Mega Drop Down Menu

Step 4: Aligning to the Right

Now, let's see how we can align our menu and the drop down content to the right edge of the navigation bar; not only the menu item, but the drop down container should also be changed.

To accomplish this, we'll add a new class called .menu_right to the parent list item, so we reset the right margin and float it to the right :

Next, let's see the drop down. In the previous CSS code, rounded corners were applied to all corners except the left-top one to, in order to match the background of the parent menu item. Now we want this drop down to stick to the right edge of the parent menu item. So, we'll overwrite the border-radius values with a new class called .align_right, and set the top-right corner to 0.

Last but not least, we want to make the drop down appear on the right; so we'll use our new class and reset the left value, then make it stick to the right :

Now it's ready to be used in the menu :

And a small preview of the code above :

Building a CSS3 Mega Drop Down Menu

Step 5: Adding and Styling Content

Now that we have the whole structure ready, we can add as much content as we want: text, lists, images, etc.

General Stylings

Let's begin with some basic styling for paragraphs and headings :

We can apply a nice blue color to the links within the drop down :

Lists Stylings

Let's revamp our lists; we have to reset some styling such as the background color or the borders which are used in the navigation bar :

Styling Images

And to create a paragraph with an image on the left :

Text Boxes

To highlight some content, here is an example of a dark box with rounded corners and a subtle inset shadow :

Restylings Lists

And to finish, here's another way to style your lists using, again, some CSS3 :


Step 6: Handling Browser Compatibility and IE6

All browsers handle hover on non-anchor tags . . . except Internet Explorer 6; so our Mega Menu is still not working with this old browser. We can fix this problem thanks to a behavior file that will add this functionality. You can find it here, and use conditional comments to target IE6 only; more explanations can be found via this article from CSS-Tricks.

To target IE6, we'll use the following code :

I've used a few PNG files in this tutorial, and, as everyone knows, IE6 doesn't support transparency so we have different solutions :

  • Convert them to GIF or PNG-8 format
  • Use a script
  • Set a background color other than the default grey with TweakPNG for example

I'll let you choose the one that fits to your needs. Now, let's review a full working example.


Final Example

HTML Part

CSS Part


Interesting and Relevant Links


Conclusion

I hope you've enjoyed this tutorial on creating mega menus. Thanks for following along!

Advertisement