HTML5 Development Center

Developed for you in part by
 
featured

Creating an Animated Drop-down Navigation Menu in HTML5/CSS3 and Webkit

By | | CSS3 | HTML | HTML5 | Programming

This is a tutorial for beginner and intermediate HTML5 and CSS3 developers who want to create a drop-down menu. The menu we’re creating is animated using only CSS … there’s no JavaScript in sight!

A demo of the outcome of this tutorial can be found here.

We’ll set out our HTML using a nav body and provide an id of “top”. The next step will be to create a few lists with a href of “#”, and give your link a class of “toplink”. Inside each of those lists we will be nesting an unordered list and then three list items for each link of navigation, including its href.

<nav id="top">
  <ul>
    <li> <a class="toplink" href="#">Item One</a>
      <ul>
        <li><a href="#">Drop Menu 1</a></li>
        <li><a href="#">Drop Menu 2</a></li>
        <li><a href="#">Drop Menu 3</a></li>
      </ul>
    </li>
    <li><a class="toplink" href="#">Item Two</a>
      <ul>
        <li><a href="#">Drop Menu 1</a></li>
        <li><a href="#">Drop Menu 2</a></li>
        <li><a href="#">Drop Menu 3</a></li>
      </ul>
    </li>
    <li><a class="toplink" href="#">Item Three</a>
      <ul>
        <li><a href="#">Drop Menu 1</a></li>
        <li><a href="#">Drop Menu 2</a></li>
        <li><a href="#">Drop Menu 3</a></li>
      </ul>
    </li>
    <li><a class="toplink" href="#">Item Four</a>
      <ul>
        <li><a href="#">Drop Menu 1</a></li>
        <li><a href="#">Drop Menu 2</a></li>
        <li><a href="#">Drop Menu 3</a></li>
      </ul>
    </li>
  </ul>
</nav>

That’s the navigation complete, but feel free to edit the text and links for your own needs.

On to the the CSS

Using the ids that we declared in the HTML we will structure our navigation bar with some color, and of course the drop-down animations (I’ve included some hover colors too).

First we will set the z-index (for stacking), width, line-height, padding and background color for the navigation box (#top):

#top {
  z-index: 2;
  width: 200px;
  line-height: 2em;
  background: #222222;
  padding: 30px;
} 

Then we’ll take the navigaiton list and list links and give them color and some padding to sit in their own boxes:

 #top li li, #top li li a {
  height:0px;
  margin-top: -10px;
  text-align: center;
  color: rgba(255,255,255,.0);
  text-decoration: none;
  -webkit-transition:all .7s ease-in-out;
 } 

Next we’ll give the drop-down menus some padding, a rounded border and a hovering color change effect:

 #top li:hover li {
  height:40px;
  margin-top:5px;
  margin-bottom: 5px;
  background: #333333;
  border-radius: 20px;
}
#top li li:first-of-type {
  padding-top: 15px;
}
#top li:hover li a {
  color: rgba(255,102,0,.8);
}
#top li li a:hover {
  color:#ffffff;
} 

Lastly we will remove bullet points from all lists on the page and give the links a nice orange color, and remove their underlines:

 li {
  list-style: none;
}
a {
  color: orange;
  text-decoration: none;
} 

Once you’ve completed the tutorial you can style the navigation all you like! This is a basic example, and you can maneuvre and redesign it to fit your website to your liking.

If you enjoyed reading this post, you’ll love Learnable; the place to learn fresh skills and techniques from the masters. Members get instant access to all of SitePoint’s ebooks and interactive online courses, like HTML5 & CSS3 for the Real World.

Learn Responsive Web Design

Join Learnable $29 Includes all SitePoint books

Jean-Pierre Gassin

Jean-Pierre Gassin is a web developer, writer and design enthusiast from the Gold Coast, Australia. He runs The GeekGrounds, a tech and gaming culture site, and is studying a Bachelor of Information Technology.

More Posts - Website - Twitter - LinkedIn - Google Plus

{ 18 comments }

Geoffrey June 28, 2012 at 4:09 pm
@artwitto June 26, 2012 at 8:31 am

Where should I place a “top” id?

Ehsan June 22, 2012 at 2:40 am

Demo?

Anant June 22, 2012 at 1:50 am

No browser compatibility.. only web-kit supported..

Umair Ulhaque June 21, 2012 at 10:38 pm

Nicely written, but i didn’t see any of HTML5 tag into this post and also there is no Demo to determine the output.

Joe Kukan June 20, 2012 at 5:13 pm

Demo please, thank you!

Alex Hall June 20, 2012 at 4:46 am

Why HTML5? Looks like plain ol’ HTML to me. No nav element to speak of, despite displaying navigation. I also agree that a demo would be useful. But thanks for the write-up :-)

Marco June 19, 2012 at 9:49 pm

Mate, no demo?

Chris Emerson June 19, 2012 at 9:28 am

Demo?

Rory June 19, 2012 at 7:29 am

I believe tutorials like these should have a demo so we can see what we are building before we start.

Annonimous June 19, 2012 at 7:24 am

What is the DEMO?

S June 19, 2012 at 7:22 am

And only -webkit? Shame…

viaria June 19, 2012 at 7:09 am

where is demo.

Andrew June 19, 2012 at 6:00 am

No demo? What about Chris’ question about compatibility?

Greg June 19, 2012 at 5:52 am

Div id of “top” isn’t set which wasn’t a hard fix but, may confuse a beginner looking to play with this. Good write up.

Chris June 19, 2012 at 5:29 am

What about browser compatibility? How does this menu degrade in non-compatible browsers? Just curious…

G June 19, 2012 at 5:22 am

As this is a menu, and is listed as an HTML5 tutorial, shouldn’t it be marked up using the Nav tag…

Sean June 19, 2012 at 5:20 am

I think you forgot an ID and maybe a class definition in the basic HTML; but maybe that’s our job to figure out :0

Comments on this entry are closed.