Advertisement
  1. Code
  2. Web Development

How to Build a Kick-Butt CSS3 Mega Dropdown Menu

Scroll to top
Read Time: 16 min
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.

Here's what you'll be building: 

mega menu examplemega menu examplemega menu example

You can jump to a live demo to see it in action.

1. Building the Navigation Bar

Let's begin with a basic menu, built with an unordered list and some basic CSS styling. Create a folder for this project, and inside it create these two files: index.html (for the HTML markup) and style.css (for the stylesheet code). Also, create a folder named img in the project root and move some images there to be used on the menu.

The directory structure looks like this:

1
my-project-folder/
2
├── index.html
3
├── style.css
4
└── img/
5
    ├── img01.png
6
    ├── img02.png
7
    ├── img03.png
8
    ├── img04.png
9
    └── and other images

Open the index.html file with your favorite text editor and include the following code:

1
<!DOCTYPE html>
2
<html lang="en">
3
    <head>
4
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
5
        <link rel="stylesheet" href="style.css" type="text/css" media="screen" />    
6
        <title>Mega Drop Down Menu</title>
7
    </head>
8
    <body>
9
        <ul id="menu">
10
            <li><a href="#">Home</a></li>
11
            <li><a href="#">About</a></li>
12
            <li><a href="#">Services</a></li>
13
            <li><a href="#">Portfolio</a></li>
14
            <li><a href="#">Contact</a></li>
15
        </ul>        
16
    </body>
17
</html>

Step 1: 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. Open your style.css file and include the following code:

1
#menu {
2
	list-style:none;
3
	width:940px;
4
	margin:30px auto 0px auto;
5
	height:43px;
6
	padding:0px 20px 0px 20px;
7
    
8
    /* More code will go here */
9
}

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 Mozilla-based browsers (like Firefox).

For rounded corners, add the following syntax in the #menu block:

1
-moz-border-radius: 10px
2
-webkit-border-radius: 10px;
3
border-radius: 10px;

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. Add the following code to the #menu block:

1
background: #014464;
2
background: -moz-linear-gradient(top, #0272a7, #013953);
3
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#0272a7), to(#013953));

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, and 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:

1
-moz-box-shadow:inset 0px 0px 1px #edf9ff;
2
-webkit-box-shadow:inset 0px 0px 1px #edf9ff;
3
box-shadow:inset 0px 0px 1px #edf9ff;

Here's the final CSS code for the #menu container, which should go in the style.css file:

1
#menu {
2
	list-style:none;
3
	width:940px;
4
	margin:30px auto 0px auto;
5
	height:43px;
6
	padding:0px 20px 0px 20px;
7
8
	/* Rounded Corners */
9
	
10
	-moz-border-radius: 10px;
11
	-webkit-border-radius: 10px;
12
	border-radius: 10px;
13
14
	/* Background color and gradients */
15
	
16
	background: #014464;
17
	background: -moz-linear-gradient(top, #0272a7, #013953);
18
	background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#0272a7), to(#013953));
19
	
20
	/* Borders */
21
	
22
	border: 1px solid #002232;
23
24
	-moz-box-shadow:inset 0px 0px 1px #edf9ff;
25
	-webkit-box-shadow:inset 0px 0px 1px #edf9ff;
26
	box-shadow:inset 0px 0px 1px #edf9ff;
27
}

Step 2: Styling Menu Items

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

1
#menu li {
2
	float:left;
3
	display:block;
4
	text-align:center;
5
	position:relative;
6
	padding: 4px 10px 4px 10px;
7
	margin-right:30px;
8
	margin-top:7px;
9
	border:none;
10
}

For the hover state and the dropdown, 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 the top corners as we'll have the dropdown sticking right under the menu items.

1
background: #F4F4F4;
2
background: -moz-linear-gradient(top, #F4F4F4, #EEEEEE);
3
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#F4F4F4), to(#EEEEEE));

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 to 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.

1
border: 1px solid #777777;
2
padding: 4px 9px 4px 9px;

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

1
-moz-border-radius: 5px 5px 0px 0px;
2
-webkit-border-radius: 5px 5px 0px 0px;
3
border-radius: 5px 5px 0px 0px;

Here is the final CSS for the menu items on hover. Add the code to your style.css file:

1
#menu li:hover {
2
	border: 1px solid #777777;
3
	padding: 4px 9px 4px 9px;
4
	
5
	/* Background color and gradients */
6
	
7
	background: #F4F4F4;
8
	background: -moz-linear-gradient(top, #F4F4F4, #EEEEEE);
9
	background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#F4F4F4), to(#EEEEEE));
10
	
11
	/* Rounded corners */
12
	
13
	-moz-border-radius: 5px 5px 0px 0px;
14
	-webkit-border-radius: 5px 5px 0px 0px;
15
	border-radius: 5px 5px 0px 0px;
16
}

For the links, we'll apply a nice text shadow using 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:

1
text-shadow: 1px 1px 1px #000;

Here is the final CSS for the links. Add it to style.css:

1
#menu li a {
2
	font-family:Arial, Helvetica, sans-serif;
3
	font-size:14px; 
4
	color: #EEEEEE;
5
	display:block;
6
	outline:0;
7
	text-decoration:none;
8
	text-shadow: 1px 1px 1px #000;
9
}

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

1
#menu li:hover a {
2
	color:#161616;
3
	text-shadow: 1px 1px 1px #FFFFFF;
4
}

Finally, we need a way to indicate if there's a dropdown or not by using a simple arrow image as a 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):

1
/* This requires that you have drop.png image in an  "img" folder which resides

2
in the project root folder*/
3
4
#menu li .drop {
5
	padding-right:21px;    
6
	background:url("img/drop.png") no-repeat right 8px;
7
}
8
#menu li:hover .drop {
9
	background:url("img/drop.png") no-repeat right 7px;
10
}

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

index.html

1
<ul id="menu">
2
    <li><a href="#">Home</a></li>
3
    <li><a href="#">About</a></li>
4
    <li><a href="#">Services</a></li>
5
    <li><a href="#">Portfolio</a></li>
6
    <li><a href="#">Contact</a></li>
7
</ul>        

menu.css

1
#menu {
2
	list-style:none;
3
	width:940px;
4
	margin:30px auto 0px auto;
5
	height:43px;
6
	padding:0px 20px 0px 20px;
7
8
	/* Rounded Corners */
9
	
10
	-moz-border-radius: 10px;
11
	-webkit-border-radius: 10px;
12
	border-radius: 10px;
13
14
	/* Background color and gradients */
15
	
16
	background: #014464;
17
	background: -moz-linear-gradient(top, #0272a7, #013953);
18
	background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#0272a7), to(#013953));
19
	
20
	/* Borders */
21
	
22
	border: 1px solid #002232;
23
24
	-moz-box-shadow:inset 0px 0px 1px #edf9ff;
25
	-webkit-box-shadow:inset 0px 0px 1px #edf9ff;
26
	box-shadow:inset 0px 0px 1px #edf9ff;
27
}
28
29
#menu li {
30
	float:left;
31
	display:block;
32
	text-align:center;
33
	position:relative;
34
	padding: 4px 10px 4px 10px;
35
	margin-right:30px;
36
	margin-top:7px;
37
	border:none;
38
}
39
40
#menu li:hover {
41
	border: 1px solid #777777;
42
	padding: 4px 9px 4px 9px;
43
	
44
	/* Background color and gradients */
45
	
46
	background: #F4F4F4;
47
	background: -moz-linear-gradient(top, #F4F4F4, #EEEEEE);
48
	background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#F4F4F4), to(#EEEEEE));
49
	
50
	/* Rounded corners */
51
	
52
	-moz-border-radius: 5px 5px 0px 0px;
53
	-webkit-border-radius: 5px 5px 0px 0px;
54
	border-radius: 5px 5px 0px 0px;
55
}
56
57
#menu li a {
58
	font-family:Arial, Helvetica, sans-serif;
59
	font-size:14px; 
60
	color: #EEEEEE;
61
	display:block;
62
	outline:0;
63
	text-decoration:none;
64
	text-shadow: 1px 1px 1px #000;
65
}
66
67
#menu li:hover a {
68
	color:#161616;
69
	text-shadow: 1px 1px 1px #FFFFFF;
70
}
71
#menu li .drop {
72
	padding-right:21px;
73
	background:url("img/drop.png") no-repeat right 8px;
74
}
75
#menu li:hover .drop {
76
	background:url("img/drop.png") no-repeat right 7px;
77
}

And the result is:

dropdown menu

2. Coding the Dropdown

All dropdowns will go inside the list item <li> elements, just beneath the <a> tags. A "classic" dropdown menu usually contains lists nested within parent list items and looks like:

1
<ul id="menu">
2
	<li><a href="#">Item 1</a><
3
		<ul>
4
			<li><a href="#">Subitem 1</a></li>
5
			<li><a href="#">Subitem 2</a></li>
6
			<li><a href="#">Subitem 3</a></li>
7
		</ul>
8
	</li>
9
	<li><a href="#">Item 2</a><
10
		<ul>
11
			<li><a href="#">Subitem 1</a></li>
12
			<li><a href="#">Subitem 2</a></li>
13
		</ul>
14
	</li>
15
</ul>

General Structure

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

1
<ul id="menu">
2
	<li><a href="#">Item 1</a>
3
		<div>
4
		Drop down Content
5
		<div>
6
	</li>
7
	<li><a href="#">Item 2</a>
8
		<div>
9
		Drop down Content
10
		<div>
11
	</li>
12
</ul>

This will be the basic structure for the dropdown. 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.

Step 1: Dropdown Containers

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

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

1
position:absolute;
2
left:-999em;

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:

1
background:#F4F4F4;
2
background: -moz-linear-gradient(top, #EEEEEE, #BBBBBB);
3
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#EEEEEE), to(#BBBBBB));

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

1
-moz-border-radius: 0px 5px 5px 5px;
2
-webkit-border-radius: 0px 5px 5px 5px;
3
border-radius: 0px 5px 5px 5px;

Here's the complete code. Add it to style.css:

1
.dropdown_1column, 
2
.dropdown_2columns, 
3
.dropdown_3columns, 
4
.dropdown_4columns,
5
.dropdown_5columns {
6
	margin:4px auto;
7
	position:absolute;
8
	left:-999em; /* Hides the drop down */
9
	text-align:left;
10
	padding:10px 5px 10px 5px;
11
	border:1px solid #777777;
12
	border-top:none;
13
	
14
	/* Gradient background */
15
	background:#F4F4F4;
16
	background: -moz-linear-gradient(top, #EEEEEE, #BBBBBB);
17
	background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#EEEEEE), to(#BBBBBB));
18
19
	/* Rounded Corners */
20
	-moz-border-radius: 0px 5px 5px 5px;
21
	-webkit-border-radius: 0px 5px 5px 5px;
22
	border-radius: 0px 5px 5px 5px;
23
}

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

menu with unwanted rounded corners

Now here is our example:

with rounded corners removed

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

In order to have a perfect dropdown container, we need to specify the width for each one. Add the following code in style.css:

1
.dropdown_1column {width: 140px;}
2
.dropdown_2columns {width: 280px;}
3
.dropdown_3columns {width: 420px;}
4
.dropdown_4columns {width: 560px;}
5
.dropdown_5columns {width: 700px;}

Finally, in style.css, to show the dropdowns on mouse hover, we'll simply use:

1
#menu li:hover .dropdown_1column, 
2
#menu li:hover .dropdown_2columns, 
3
#menu li:hover .dropdown_3columns,
4
#menu li:hover .dropdown_4columns,
5
#menu li:hover .dropdown_5columns {
6
	left:-1px;top:auto;
7
}

Step 2: Using the Dropdown Containers

Our classes are ready to be included in our menu. We'll use each one of them, from the five-column layout to the single-column dropdown. Inside index.html, replace just the ul element and all of its content with this:

1
<ul id="menu">
2
	<li><a href="#">Home</a></li>
3
	<li><a href="#" class="drop">5 Columns</a>
4
		<div class="dropdown_5columns">
5
		<p>5 Columns content</p>
6
		</div>
7
	</li>
8
	<li><a href="#" class="drop">4 Columns</a>
9
		<div class="dropdown_4columns">
10
		<p>4 Columns content</p>
11
		</div>
12
	</li>
13
	<li><a href="#" class="drop">3 Columns</a>
14
		<div class="dropdown_3columns">
15
		<p>3 Columns content</p>
16
		</div>
17
	</li>
18
	<li><a href="#" class="drop">2 Columns</a>
19
		<div class="dropdown_2columns">
20
		<p>2 Columns content</p>
21
		</div>
22
	</li>
23
	<li><a href="#" class="drop">1 Column</a>
24
		<div class="dropdown_1column">
25
		<p>1 Column content</p>
26
		</div>
27
	</li>
28
</ul>

Here is a preview of the code above:

dropdown in action

3. Creating the Dropdown Container Columns

Now that we have the containers ready, we'll create columns of increasing sizes, following the principles of the 960 grid system. Include this in style.css:

1
.col_1,
2
.col_2,
3
.col_3,
4
.col_4,
5
.col_5 {
6
	display:inline;
7
	float: left;
8
	position: relative;
9
	margin-left: 5px;
10
	margin-right: 5px;
11
}
12
.col_1 {width:130px;}
13
.col_2 {width:270px;}
14
.col_3 {width:410px;}
15
.col_4 {width:550px;}
16
.col_5 {width:690px;}

Using Columns

Here is an example of a dropdown containing several columns. In this example, we have different combinations using all kinds of columns. The following is the code for the second <li> element in the menu:

1
<ul id="menu">
2
    <!--<li></li>-->
3
	<li><a href="#" class="drop">5 Columns</a>
4
		<div class="dropdown_5columns">
5
			<div class="col_5">
6
			<p>This is a 5 Columns content</p>
7
			</div>
8
			<div class="col_1">
9
			<p>This is a 1 Column content</p>
10
			</div>
11
			<div class="col_1">
12
			<p>This is a 1 Column content</p>
13
			</div>
14
			<div class="col_1">
15
			<p>This is a 1 Column content</p>
16
			</div>
17
			<div class="col_1">
18
			<p>This is a 1 Column content</p>
19
			</div>
20
			<div class="col_1">
21
			<p>This is a 1 Column content</p>
22
			</div>
23
			<div class="col_4">
24
			<p>This is a 4 Columns content</p>
25
			</div>
26
			<div class="col_1">
27
			<p>This is a 1 Column content</p>
28
			</div>
29
			<div class="col_3">
30
			<p>This is a 3 Columns content</p>
31
			</div>
32
			<div class="col_2">
33
			<p>This is a 2 Columns content</p>
34
			</div>
35
		</div>
36
	</li>
37
</ul>

Code preview:

preview of columns

4. Aligning to the Right

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

To accomplish this, in style.css, 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:

1
#menu .menu_right {
2
	float:right;
3
	margin-right:0px;
4
}

Next, let's see the dropdown. In the previous CSS code, rounded corners were applied to all corners except the top-left one, in order to match the background of the parent menu item. Now we want this dropdown 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.

1
#menu li .align_right {
2
	/* Rounded Corners */
3
	-moz-border-radius: 5px 0px 5px 5px;
4
	-webkit-border-radius: 5px 0px 5px 5px;
5
	border-radius: 5px 0px 5px 5px;
6
}

Last but not least, we want to make the dropdown appear on the right. So we'll use our new class and reset the left value, and then make it stick to the right:

1
#menu li:hover .align_right {
2
	left:auto;
3
	right:-1px;
4
	top:auto;
5
}

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

1
<li class="menu_right"><a href="#" class="drop">Right</a>
2
	<div class="dropdown_1column align_right">
3
		<div class="col_1">
4
		<p>This is a 1 Column content</p>
5
		</div>
6
	</div>
7
</li>

Here's a small preview of the code above:

Building a CSS3 Mega Drop Down Menu

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.

Step 1: General Styling

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

1
#menu p, #menu h2, #menu h3, #menu ul li {
2
	font-family:Arial, Helvetica, sans-serif;
3
	line-height:21px;
4
	font-size:12px;
5
	text-align:left;
6
	text-shadow: 1px 1px 1px #FFFFFF;
7
}
8
#menu h2 {
9
	font-size:21px;
10
	font-weight:400;
11
	letter-spacing:-1px;
12
	margin:7px 0 14px 0;
13
	padding-bottom:14px;
14
	border-bottom:1px solid #666666;
15
}
16
#menu h3 {
17
	font-size:14px;
18
	margin:7px 0 14px 0;
19
	padding-bottom:7px;
20
	border-bottom:1px solid #888888;
21
}
22
#menu p {
23
	line-height:18px;
24
	margin:0 0 10px 0;
25
}
26
.strong {
27
	font-weight:bold;
28
}
29
.italic {
30
	font-style:italic;
31
}

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

1
#menu li:hover div a {
2
	font-size:12px;
3
	color:#015b86;
4
}
5
#menu li:hover div a:hover {
6
	color:#029feb;
7
}

Step 2: List Styling

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

1
#menu li ul {
2
	list-style:none;
3
	padding:0;
4
	margin:0 0 12px 0;
5
}
6
#menu li ul li {
7
	font-size:12px;
8
	line-height:24px;
9
	position:relative;
10
	text-shadow: 1px 1px 1px #ffffff;
11
	padding:0;
12
	margin:0;
13
	float:none;
14
	text-align:left;
15
	width:130px;
16
}
17
#menu li ul li:hover {
18
	background:none;
19
	border:none;
20
	padding:0;
21
	margin:0;
22
}

Step 3: Styling Images

1
.imgshadow {
2
	background:#FFFFFF;
3
	padding:4px;
4
	border:1px solid #777777;
5
	margin-top:5px;
6
	-moz-box-shadow:0px 0px 5px #666666;
7
	-webkit-box-shadow:0px 0px 5px #666666;
8
	box-shadow:0px 0px 5px #666666;
9
}

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

1
.img_left {
2
	width:auto;
3
	float:left;
4
	margin:5px 15px 5px 5px;
5
}

Step 4: Text Boxes

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

1
#menu li .black_box {
2
	background-color:#333333;
3
	color: #eeeeee;
4
	text-shadow: 1px 1px 1px #000;
5
	padding:4px 6px 4px 6px;
6
7
	/* Rounded Corners */
8
	-moz-border-radius: 5px;
9
	-webkit-border-radius: 5px;
10
	border-radius: 5px;
11
12
	/* Shadow */
13
	-webkit-box-shadow:inset 0 0 3px #000000;
14
	-moz-box-shadow:inset 0 0 3px #000000;
15
	box-shadow:inset 0 0 3px #000000;
16
}

Step 5: Restyling Lists

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

1
#menu li .greybox li {
2
	background:#F4F4F4;
3
	border:1px solid #bbbbbb;
4
	margin:0px 0px 4px 0px;
5
	padding:4px 6px 4px 6px;
6
	width:116px;
7
8
	/* Rounded Corners */
9
	-moz-border-radius: 5px;
10
	-webkit-border-radius: 5px;
11
	border-radius: 5px;
12
}
13
#menu li .greybox li:hover {
14
	background:#ffffff;
15
	border:1px solid #aaaaaa;
16
	padding:4px 6px 4px 6px;
17
	margin:0px 0px 4px 0px;
18
}

Final Example

Here's a live demo of what you'll be building:

Conclusion

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

Also, if you need a quick starting point for building a professional website, then browse through our gallery of CSS templates. They offer a number of ready-to-use professional features.

Advertisement
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.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.