Animate Your Dropdown Navigation Menu with CSS3
Dropdown menus can almost be considered a web standard, and occur for example in many WordPress Themes.
Written in 2012. Some details, links or tools may be outdated, but I've kept it here for the record.
Dropdown menus can almost be considered a web standard, and occur for example in many WordPress Themes. Now I’ve worked some times with Superfish plugin, which makes the transition between drop-down menu’s smoother, but it did not work optimal in all cases. Well, with CSS3, it is fairly easy to make a smoothly animated dropdown menu!
The good thing is, that we don’t need Javascript. The bad thing is, the animation will not work in older versions of IE. Well, I guess you would have expected that. Nevertheless, it saves a whole piece of Javascript and rewards users of normal browsers with supreme experience. This tutorial is based on the following basic menu structure, as often present in WordPress Themes
The Basic HTML Structure
For this tutorial, I assume a dropdown set-up which is similar as showed below:
<html>
<head>
<title>Animate your drop-down menu using CSS3</title>
<link href="assets/css/style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<nav class="main-navigation">
<ul>
<li>
<a href="#">A Menu-Item with Dropdown</a>
<ul class=dropdown-menu>
<li><a href="#">Dropdown Item 1</a></li>
<li><a href="#">Dropdown Item 2</a></li>
</ul>
</li>
<li>
<a href="#">Another Menu-Item</a>
</li>
</ul>
</nav>
</body>
</html>
Initial CSS set-up & CSS Transitions
The following CSS rules determine the basic styling of the dropdown menu. As you can see, it is just some comming css.
/* Link color and fonts */
.main-navigation a {
color: #fff;
display: block;
font-family: Arial, sans-serif;
padding: 10px 20px;
text-decoration: none;
}
/* Reset the ul styles */
.main-navigation ul {
list-style: none;
margin: 0;
padding: 0;
}
/* Styling of each menu item, allows the absolute positioning of the li's ul children. */
.main-navigation li {
background: #6e9ef8;
float: left;
position: relative;
}
/* The dropdown menu most important features, making it invisible at default. */
ul.dropdown-menu {
background: #fff;
color: #000;
display: none;
font-size: 13px;
left: 0;
position: absolute;
width: 100%;
}
/**
* The Styling of dropdown menu-items
*/
ul.dropdown-menu li {
display: block;
float: none;
}
Why CSS Transitions will not work
Now I’ve written earlier about CSS3 transitions which can enhance the navigation experience in websites, by adding a transition time between different states (e.g. the transition from non-hover to hover takes 1 second to complete with an eased-out animation, allowing for example the smooth transition of color). Now the problem is that transition will not accept a transition between the css attributes display:none and display:block, so we can not use transition here. We might evade this by using the opacity attribute for example, but this gives problems with DropDown menus with more levels, as technically every child is present with an opacity value of zero, making it impossible to navigate through second-child level drop-down menu’s. So we have to use display: block;
Using CSS3 Animation
However, with CSS3 animation we can solve this problem! We just need to start a simple animates that animates opacity from 0 to 1 when we hover one of the menu-items. One small recap about CSS3 animations! This is the shorthand notation of CSS3 animation (excluding all the browser prefixes for now). W3schools give a terrible good explanation of animation, so if you are still unknown in this area, I will suggest to look there.
animation:
<animation-name>
<animation-duration>
<animation-timing-function>
<animation-delay>
<animation-iteration-count>
<animation-direction>
<animation-play-state>;
Now we still need to define an animation using keyframes. This is basically how it works:
@keyframes animation-name {
from { property: value; } to { property: value; }
}
Now, using this knowledge, we can easily use this to animate our opacity within the Dropdown Menu! We first have to add some lines of code to what happens when we hover. Then, we just need to define our keyframes with the right name and values and we are done! As you can see we animate from opacity 0 to opacity 1, and we used display: block to display the menu again.
/**
* Targets all ul that are a direct child of any ul li:hover in the .main-navigation container,
* Also the animation properties!
* The animation is called opacity, takes 700ms,
* easing is ease-in-out, no delay, it will run one time in normal direction
*/
.main-navigation ul li:hover > ul.dropdown-menu {
display: block; /* Dropdown will be visible after hover. */
opacity: 1; /* This keeps the opacity at 1 after the animation has run (e.g. could be considered as the final or default stage of the element) */
animation: opacity 700ms ease-in-out normal;
-moz-animation: opacity 700ms ease-in-out normal;
-ms-animation: opacity 700ms ease-in-out normal;
-o-animation: opacity 700ms ease-in-out normal;
-webkit-animation: opacity 700ms ease-in-out normal;
}
/**
* Defines how the animation will look like
*/
@keyframes opacity {
from { opacity:0; }
to { opacity: 1; }
}
@-moz-keyframes opacity {
from { opacity: 0; }
to { opacity: 1; }
}
@-ms-keyframes opacity {
from {opacity:0; }
to { opacity: 1; }
}
@-o-keyframes opacity {
from {opacity:0; }
to { opacity: 1; }
}
@-webkit-keyframes opacity {
from { opacity:0; }
to { opacity: 1; }
}
That’s it, using these animations the dropdown it’s opacity is animated. Of course the use of animation is much wider. It can be used to animate loading screens, flash like movement of website parts and so on. Of course there are a lot of other css properties to animate. Ever thought about position? Or transform scale? Or colors? The possibilities are (almost) endless, as animate.css by Dan Eden proves.