Pure CSS is more than enough to create incredible web animations. These can range from sliders to carousels and even dropdown navigation menus.
But the trickiest part about CSS animation is learning how it all works. In this tutorial, I’ll guide you through the process of building a slide-in dropdown navigation menu from very simple code.
The nav submenu links will slide in from the right-hand side of the screen and disappear when hidden.
Take a look at the live demo below to see how this works in your browser.
I’ve re-built an effect from CodePen originally made by Andreas Eracleous. I’ve redesigned the nav bar to not only be larger and fullscreen but to also stay fixed while scrolling down the page.
Let’s start with the raw HTML and work our way to the animations
First I’m creating a very basic HTML5 doctype. In the head section, I’ll add a link to my own stylesheet named styles.css, along with a CDN-hosted link to Normalize.css.
This just makes the design a lot easier to manage without re-writing lines of code.
In the body you’ll find some basic HTML5 elements containing a simple unordered list. Each list item contains a link and the dropdown items contain a secondary UL element.
Note the links don’t lead anywhere, but this is easy to change in a real menu. I’ve kept all links hashed to keep visitors on the demo page.
The header element acts as an overall 100% width container.
The internal nav element is similar but can be set up with a fixed width to center the content. So if your layout never goes beyond 900px you can set max-width: 900px to the nav element.
For this demo, I’ve expanded the menu to take up the entirety of the window.
Lastly, I have a div with the class .content holding lorem ipsum paragraphs. This does not affect the menu’s operation, but it does showcase the ability to scroll and maintain a fixed menu at the top of the page.
Base CSS Styles
Moving over to my styles.css file is where the real action happens.
This code uses natural CSS animation with keyframes and lots of pseudo-classes where appropriate. The syntax is at least intermediate-level CSS but it’s easy to understand with some patience.
First I have all the basic CSS page structure with a UTF-8 charset and the Ubuntu Google Webfont.
You’ll notice that the header with a class .topbar is fixed to the page. This allows the full menu to stay fixed as you scroll down the page.
The next block of code defines how the menu should behave.
I’m using ul.nav to identify this one particular unordered list with a class, otherwise, we’d be targeting every unordered list on the page.
/** navigation style **/
ul.nav {
display: block;
float: left;
width: 100%;
margin: 0;
padding: 0;
list-style: none;
font-family: 'Ubuntu', Helvetica, sans-serif;
font-size: 1.1em;
text-transform: uppercase;
font-weight: bold;
background: #474747;
}
ul.nav > li {
display: block;
float: left;
line-height: 64px;
padding: 0 20px;
}
ul li a {
position: relative;
text-decoration: none;
color: #999;
}
ul.nav li > ul {
position: absolute;
display: none;
width: 100%;
z-index: 99;
}
ul.nav li > ul li {
position: relative;
display: block;
float: left;
color: #fff;
z-index: 999;
}
ul.nav li > ul li a {
color: #eee;
display: block;
padding: 0 20px;
line-height: 70px;
}
ul.nav li > ul li a:hover { color: #474747; }
ul.nav > li:hover > a { color: #ececec; }
ul.nav li:hover > ul {
display: block;
}
Every first-level list item has a line-height of 64px. This is my chosen height for the menu itself – obviously, you can adjust as needed.
The .nav li > ul selector targets the very first unordered list found within a list item. This would contain a secondary menu of links that appear in the dropdown, so it’s hidden by default with absolute positioning.
Since we have these elements positioned we can use the z-index property.
This forces all sub-menu list items to stay on top of the unordered list(so links appear on top of the blue background).
Animated Action
In the latter portion of my CSS, you’ll find the animation code. Keep in mind there are two subtle animations: first, the sub-menu drops down into view, then the sub-links slide into view from the right side of the page.
Each animation is later defined using keyframes, but you can see they have individual names applied to the appropriate elements. nav-anim-left animates the list items to the left, while nav-down animates the submenu navbar down into view.
One more advanced feature is the use of :before and :after pseudo-classes on the sub-menu list.
Both parts comprise a full 100% width block with the same background color. One appears before the links, the other appears after them.
Since the menu is positioned absolute it can’t fit the 100% width by default. So this little trick forces a full-size background onto the element.
It also allows the user to hover their mouse across the entire blue bar without auto-closing the submenu.
Another neat CSS effect is the creation of arrow icons. These use the border transformation hack in CSS to create a border that gets cut off by positioning.
The result is a downward-pointing arrow which you’ll notice directly next to dropdown links, and underneath the links when the submenu is open.
I hope this tutorial offers an interesting glimpse into CSS development. With modern CSS animations, you can build practically anything with a high level of accuracy and browser support.
Feel free to download a copy of my source code and play around in your own projects. And if you build anything cool on top of this code feel free to link us in the discussion area.