An Interest In:
Web News this Week
- March 23, 2024
- March 22, 2024
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
How to Build a Kick-Butt CSS3 Mega Drop-Down Menu
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:
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:
my-project-folder/
├── index.html
├── style.css
└── img/
├── img01.png
├── img02.png
├── img03.png
├── img04.png
└── and other images
Open the index.html file with your favorite text editor and include the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<title>Mega Drop Down Menu</title>
</head>
<body>
<ul id="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Contact</a></li>
</ul>
</body>
</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 following code:
#menu {
list-style:none;
width:940px;
margin:30px auto 0px auto;
height:43px;
padding:0px 20px 0px 20px;
/* More code will go here */
}
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, add the following syntax in the #menu block:
-moz-border-radius: 10px
-webkit-border-radius: 10px;
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:
background: #014464;
background: -moz-linear-gradient(top, #0272a7, #013953);
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, 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 :
-moz-box-shadow:inset 0px 0px 1px #edf9ff;
-webkit-box-shadow:inset 0px 0px 1px #edf9ff;
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:
#menu {
list-style:none;
width:940px;
margin:30px auto 0px auto;
height:43px;
padding:0px 20px 0px 20px;
/* Rounded Corners */
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
/* Background color and gradients */
background: #014464;
background: -moz-linear-gradient(top, #0272a7, #013953);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#0272a7), to(#013953));
/* Borders */
border: 1px solid #002232;
-moz-box-shadow:inset 0px 0px 1px #edf9ff;
-webkit-box-shadow:inset 0px 0px 1px #edf9ff;
box-shadow:inset 0px 0px 1px #edf9ff;
}
Step 2: 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) :
#menu li {
float:left;
display:block;
text-align:center;
position:relative;
padding: 4px 10px 4px 10px;
margin-right:30px;
margin-top:7px;
border:none;
}
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.
background: #F4F4F4;
background: -moz-linear-gradient(top, #F4F4F4, #EEEEEE);
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 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.
border: 1px solid #777777;
padding: 4px 9px 4px 9px;
Then, we add rounded corners to the top only so the drop down will stick perfectly under the parent menu item :
-moz-border-radius: 5px 5px 0px 0px;
-webkit-border-radius: 5px 5px 0px 0px;
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:
#menu li:hover {
border: 1px solid #777777;
padding: 4px 9px 4px 9px;
/* Background color and gradients */
background: #F4F4F4;
background: -moz-linear-gradient(top, #F4F4F4, #EEEEEE);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#F4F4F4), to(#EEEEEE));
/* Rounded corners */
-moz-border-radius: 5px 5px 0px 0px;
-webkit-border-radius: 5px 5px 0px 0px;
border-radius: 5px 5px 0px 0px;
}
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 :
text-shadow: 1px 1px 1px #000;
Here is the final CSS for the links. Add to style.css:
#menu li a {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
color: #EEEEEE;
display:block;
outline:0;
text-decoration:none;
text-shadow: 1px 1px 1px #000;
}
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. Add to style.css:
#menu li:hover a {
color:#161616;
text-shadow: 1px 1px 1px #FFFFFF;
}
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) :
/* This requires that you have drop.png image in an "img" folder which resides
in the project root folder*/
#menu li .drop {
padding-right:21px;
background:url("img/drop.png") no-repeat right 8px;
}
#menu li:hover .drop {
background:url("img/drop.png") no-repeat right 7px;
}
Here is our final code for the menu container and links; only the "home" item doesn't have any drop down content for now :
index.html
<ul id="menu">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Contact</a></li>
</ul>
menu.css
#menu {
list-style:none;
width:940px;
margin:30px auto 0px auto;
height:43px;
padding:0px 20px 0px 20px;
/* Rounded Corners */
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
/* Background color and gradients */
background: #014464;
background: -moz-linear-gradient(top, #0272a7, #013953);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#0272a7), to(#013953));
/* Borders */
border: 1px solid #002232;
-moz-box-shadow:inset 0px 0px 1px #edf9ff;
-webkit-box-shadow:inset 0px 0px 1px #edf9ff;
box-shadow:inset 0px 0px 1px #edf9ff;
}
#menu li {
float:left;
display:block;
text-align:center;
position:relative;
padding: 4px 10px 4px 10px;
margin-right:30px;
margin-top:7px;
border:none;
}
#menu li:hover {
border: 1px solid #777777;
padding: 4px 9px 4px 9px;
/* Background color and gradients */
background: #F4F4F4;
background: -moz-linear-gradient(top, #F4F4F4, #EEEEEE);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#F4F4F4), to(#EEEEEE));
/* Rounded corners */
-moz-border-radius: 5px 5px 0px 0px;
-webkit-border-radius: 5px 5px 0px 0px;
border-radius: 5px 5px 0px 0px;
}
#menu li a {
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
color: #EEEEEE;
display:block;
outline:0;
text-decoration:none;
text-shadow: 1px 1px 1px #000;
}
#menu li:hover a {
color:#161616;
text-shadow: 1px 1px 1px #FFFFFF;
}
#menu li .drop {
padding-right:21px;
background:url("img/drop.png") no-repeat right 8px;
}
#menu li:hover .drop {
background:url("img/drop.png") no-repeat right 7px;
}
And the result is :
2. Coding the Drop Down
All dropdowns will go inside the list item <li>
elements, just beneath the <a>
tags. A "classic" drop down menu usually contains lists nested within parent list items and looks like:
<ul id="menu">
<li><a href="#">Item 1</a><
<ul>
<li><a href="#">Subitem 1</a></li>
<li><a href="#">Subitem 2</a></li>
<li><a href="#">Subitem 3</a></li>
</ul>
</li>
<li><a href="#">Item 2</a><
<ul>
<li><a href="#">Subitem 1</a></li>
<li><a href="#">Subitem 2</a></li>
</ul>
</li>
</ul>
General Structure
For our Mega Menu, instead of nested lists, we'll simply use standard DIVs, which will work like any nested list :
<ul id="menu">
<li><a href="#">Item 1</a>
<div>
Drop down Content
<div>
</li>
<li><a href="#">Item 2</a>
<div>
Drop down Content
<div>
</li>
</ul>
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.
Step 1: 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 :
position:absolute;
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:
background:#F4F4F4;
background: -moz-linear-gradient(top, #EEEEEE, #BBBBBB);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#EEEEEE), to(#BBBBBB));
We'll again use rounded corners, except for the top left one:
-moz-border-radius: 0px 5px 5px 5px;
-webkit-border-radius: 0px 5px 5px 5px;
border-radius: 0px 5px 5px 5px;
Here's the complete code. Add it in style.css:
.dropdown_1column,
.dropdown_2columns,
.dropdown_3columns,
.dropdown_4columns,
.dropdown_5columns {
margin:4px auto;
position:absolute;
left:-999em; /* Hides the drop down */
text-align:left;
padding:10px 5px 10px 5px;
border:1px solid #777777;
border-top:none;
/* Gradient background */
background:#F4F4F4;
background: -moz-linear-gradient(top, #EEEEEE, #BBBBBB);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#EEEEEE), to(#BBBBBB));
/* Rounded Corners */
-moz-border-radius: 0px 5px 5px 5px;
-webkit-border-radius: 0px 5px 5px 5px;
border-radius: 0px 5px 5px 5px;
}
To illustrate this, let's see how it would look if we hadn't paid attention to detail:
Now here is our example:
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. Add the following code in style.css:
.dropdown_1column {width: 140px;}
.dropdown_2columns {width: 280px;}
.dropdown_3columns {width: 420px;}
.dropdown_4columns {width: 560px;}
.dropdown_5columns {width: 700px;}
Finally in style.css, to show the drop downs on mouse hover, we'll simply use:
#menu li:hover .dropdown_1column,
#menu li:hover .dropdown_2columns,
#menu li:hover .dropdown_3columns,
#menu li:hover .dropdown_4columns,
#menu li:hover .dropdown_5columns {
left:-1px;top:auto;
}
Step 2: 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. Inside index.html, replace just the ul
element and all of its content with this:
<ul id="menu">
<li><a href="#">Home</a></li>
<li><a href="#" class="drop">5 Columns</a>
<div class="dropdown_5columns">
<p>5 Columns content</p>
</div>
</li>
<li><a href="#" class="drop">4 Columns</a>
<div class="dropdown_4columns">
<p>4 Columns content</p>
</div>
</li>
<li><a href="#" class="drop">3 Columns</a>
<div class="dropdown_3columns">
<p>3 Columns content</p>
</div>
</li>
<li><a href="#" class="drop">2 Columns</a>
<div class="dropdown_2columns">
<p>2 Columns content</p>
</div>
</li>
<li><a href="#" class="drop">1 Column</a>
<div class="dropdown_1column">
<p>1 Column content</p>
</div>
</li>
</ul>
Here is a preview of the code above :
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. Include this in style.css:
.col_1,
.col_2,
.col_3,
.col_4,
.col_5 {
display:inline;
float: left;
position: relative;
margin-left: 5px;
margin-right: 5px;
}
.col_1 {width:130px;}
.col_2 {width:270px;}
.col_3 {width:410px;}
.col_4 {width:550px;}
.col_5 {width:690px;}
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. The following is the code for the second <li>
element in the menu:
<ul id="menu">
<!--<li></li>-->
<li><a href="#" class="drop">5 Columns</a>
<div class="dropdown_5columns">
<div class="col_5">
<p>This is a 5 Columns content</p>
</div>
<div class="col_1">
<p>This is a 1 Column content</p>
</div>
<div class="col_1">
<p>This is a 1 Column content</p>
</div>
<div class="col_1">
<p>This is a 1 Column content</p>
</div>
<div class="col_1">
<p>This is a 1 Column content</p>
</div>
<div class="col_1">
<p>This is a 1 Column content</p>
</div>
<div class="col_4">
<p>This is a 4 Columns content</p>
</div>
<div class="col_1">
<p>This is a 1 Column content</p>
</div>
<div class="col_3">
<p>This is a 3 Columns content</p>
</div>
<div class="col_2">
<p>This is a 2 Columns content</p>
</div>
</div>
</li>
</ul>
Code preview :
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, 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 :
#menu .menu_right {
float:right;
margin-right:0px;
}
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.
#menu li .align_right {
/* Rounded Corners */
-moz-border-radius: 5px 0px 5px 5px;
-webkit-border-radius: 5px 0px 5px 5px;
border-radius: 5px 0px 5px 5px;
}
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 :
#menu li:hover .align_right {
left:auto;
right:-1px;
top:auto;
}
Now it's ready to be used in the menu :
<li class="menu_right"><a href="#" class="drop">Right</a>
<div class="dropdown_1column align_right">
<div class="col_1">
<p>This is a 1 Column content</p>
</div>
</div>
</li>
And a small preview of the code above :
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 Stylings
Let's begin with some basic styling for paragraphs and headings :
#menu p, #menu h2, #menu h3, #menu ul li {
font-family:Arial, Helvetica, sans-serif;
line-height:21px;
font-size:12px;
text-align:left;
text-shadow: 1px 1px 1px #FFFFFF;
}
#menu h2 {
font-size:21px;
font-weight:400;
letter-spacing:-1px;
margin:7px 0 14px 0;
padding-bottom:14px;
border-bottom:1px solid #666666;
}
#menu h3 {
font-size:14px;
margin:7px 0 14px 0;
padding-bottom:7px;
border-bottom:1px solid #888888;
}
#menu p {
line-height:18px;
margin:0 0 10px 0;
}
.strong {
font-weight:bold;
}
.italic {
font-style:italic;
}
We can apply a nice blue color to the links within the drop down :
#menu li:hover div a {
font-size:12px;
color:#015b86;
}
#menu li:hover div a:hover {
color:#029feb;
}
Step 2: 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 :
#menu li ul {
list-style:none;
padding:0;
margin:0 0 12px 0;
}
#menu li ul li {
font-size:12px;
line-height:24px;
position:relative;
text-shadow: 1px 1px 1px #ffffff;
padding:0;
margin:0;
float:none;
text-align:left;
width:130px;
}
#menu li ul li:hover {
background:none;
border:none;
padding:0;
margin:0;
}
Step 3: Styling Images
.imgshadow {
background:#FFFFFF;
padding:4px;
border:1px solid #777777;
margin-top:5px;
-moz-box-shadow:0px 0px 5px #666666;
-webkit-box-shadow:0px 0px 5px #666666;
box-shadow:0px 0px 5px #666666;
}
And to create a paragraph with an image on the left :
.img_left {
width:auto;
float:left;
margin:5px 15px 5px 5px;
}
Step 4: Text Boxes
To highlight some content, here is an example of a dark box with rounded corners and a subtle inset shadow :
#menu li .black_box {
background-color:#333333;
color: #eeeeee;
text-shadow: 1px 1px 1px #000;
padding:4px 6px 4px 6px;
/* Rounded Corners */
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
/* Shadow */
-webkit-box-shadow:inset 0 0 3px #000000;
-moz-box-shadow:inset 0 0 3px #000000;
box-shadow:inset 0 0 3px #000000;
}
Step 5: Restyling Lists
And to finish, here's another way to style your lists using, again, some CSS3:
#menu li .greybox li {
background:#F4F4F4;
border:1px solid #bbbbbb;
margin:0px 0px 4px 0px;
padding:4px 6px 4px 6px;
width:116px;
/* Rounded Corners */
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
#menu li .greybox li:hover {
background:#ffffff;
border:1px solid #aaaaaa;
padding:4px 6px 4px 6px;
margin:0px 0px 4px 0px;
}
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 :
<!--[if IE 6]>
<style>
body {behavior: url("csshover3.htc");}
</style>
<![endif]-->
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
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 to building a professional website, then browse through our gallery of CSS Templates. They offer a number of ready to use, professional features.
Original Link: https://code.tutsplus.com/tutorials/how-to-build-a-kick-butt-css3-mega-drop-down-menu--net-15129
TutsPlus - Code
Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.More About this Source Visit TutsPlus - Code