Web designers and developers find themselves creating dropdown menus over and over. I’ve drilled dropdown menu production to a science. Seriously. Here’s the benefits of Brian Cray’s basic dropdown menu code:
Here’s what the basic dropdown looks like
Let’s go over my dropdown menu code first as it provides the basis for the fanciest dropdown menu you ever saw.
You’ve seen this kind of thing a million times. But take notice of the two UL classes: tabs and dropdown.
01.<ul class="tabs">02.<li><a href="#">Dropdown 1</a>03.<ul class="dropdown">04.<li><a href="#">Menu item 1</a>05.<ul class="dropdown">06.<li><a href="#">Submenu item 1</a></li>07.<li><a href="#">Submenu item 1</a></li>08.<li><a href="#">Submenu item 1</a></li>09.</ul>10.</li>11.<li><a href="#">Menu item 2</a></li>12.<li><a href="#">Menu item 3</a></li>13.<li><a href="#">Menu item 4</a></li>14.<li><a href="#">Menu item 5</a></li>15.<li><a href="#">Menu item 6</a></li>16.</ul>17.</li>18.<li><a href="#">Dropdown 2</a>19.<ul class="dropdown">20.<li><a href="#">Menu item 1</a></li>21.<li><a href="#">Menu item 2</a></li>22.<li><a href="#">Menu item 3</a></li>23.<li><a href="#">Menu item 4</a></li>24.<li><a href="#">Menu item 5</a></li>25.<li><a href="#">Menu item 6</a></li>26.</ul>27.</li>28.<li><a href="#">Dropdown 3</a>29.<ul class="dropdown">30.<li><a href="#">Menu item 1</a></li>31.<li><a href="#">Menu item 2</a></li>32.<li><a href="#">Menu item 3</a></li>33.<li><a href="#">Menu item 4</a></li>34.<li><a href="#">Menu item 5</a></li>35.<li><a href="#">Menu item 6</a></li>36.</ul>37.</li>38.</ul>The tabs class makes the first UL a horizontal tab bar. The dropdown class makes the other UL dropdown menus. Simple, right?
This CSS sets and resets the necessary styles so you don’t have to worry about your menu inheriting a bunch of crap that will make the menu look unexpected.
01./* tabs02.*************************/03. 04.ul.tabs05.{06.display: table;07.margin: 0;08.padding: 0;09.list-style: none;10.position: relative;11.}12. 13.ul.tabs li14.{15.margin: 0;16.padding: 0;17.list-style: none;18.display: table-cell;19.float: left;20.position: relative;21.}22. 23.ul.tabs a24.{25.position: relative;26.display: block;27.}28. 29./* dropdowns30.*************************/31. 32.ul.dropdown33.{34.margin: 0;35.padding: 0;36.display: block;37.position: absolute;38.z-index: 999;39.top: 100%;40.width: 250px;41.display: none;42.left: 0;43.}44. 45.ul.dropdown ul.dropdown46.{47.top: 0;48.left: 95%;49.}50. 51.ul.dropdown li52.{53.margin: 0;54.padding: 0;55.float: none;56.position: relative;57.list-style: none;58.display: block;59.}60. 61.ul.dropdown li a62.{63.display: block;64.}The jQuery behind this is genius. It will pick up ANY dropdown class on the page and turn it into a working dropdown menu.
01.$(function () {02.$('.dropdown').each(function () {03.$(this).parent().eq(0).hover(function () {04.$('.dropdown:eq(0)', this).show();05.}, function () {06.$('.dropdown:eq(0)', this).hide();07.});08.});09.});And that’s all you need to add to each project to create dropdown menus in a flash! Now lets take this concept a step farther and REALLY light up the show.
Here’s what the fancy menu looks like that we’ll create below
The first thing to do to make something fancy is to add colors and images.




<span> element around the main menu items01.<div id="menu">02.<ul class="tabs">03.<li><h4><a href="#">In the blog »</a></h4></li>04.<li class="hasmore"><a href="#"><span>Recent</span></a>05.<ul class="dropdown">06.<li><a href="#">Menu item 1</a></li>07.<li><a href="#">Menu item 2</a></li>08.<li><a href="#">Menu item 3</a></li>09.<li><a href="#">Menu item 4</a></li>10.<li><a href="#">Menu item 5</a></li>11.<li class="last"><a href="#">Menu item 6</a></li>12.</ul>13.</li>14.<li class="hasmore"><a href="#"><span>Topics</span></a>15.<ul class="dropdown">16.<li><a href="#">Topic 1</a></li>17.<li><a href="#">Topic 2</a></li>18.<li><a href="#">Topic 3</a></li>19.<li class="last"><a href="#">Topic 4</a></li>20.</ul>21.</li>22.<li><a href="#"><span><strong><img src="images/feed-icon-14x14.png" width="14" height="14" alt="RSS"> Subscribe to RSS</strong></span></a></li>23.<li><h4><a href="#">Elsewhere »</a></h4></li>24.<li><a href="#"><span>About</span></a></li>25.<li class="hasmore"><a href="/about/#networks"><span>Networks</span></a>26.<ul class="dropdown">27.<li><a href="#">Twitter</a></li>28.<li><a href="#">posterous</a></li>29.<li><a href="#">SpeakerSite</a></li>30.<li><a href="#">LinkedIn</a></li>31.<li class="last"><a href="#">See more…</a></li>32.</ul>33.</li>34.<li><a href="#"><span>Bookmarks</span></a></li>35.</ul>36.</div>* {margin:0; padding:0} simple reset hack (I know, there are better resets)001./* hack reset (for production, use Yahoo! reset CSS)002.*************************/003. 004.*005.{006.margin: 0;007.padding: 0;008.}009. 010./* body011.*************************/012. 013.body014.{015.font: 14px/21px Georgia, serif;016.color: #370707;017.background: #e3d79b url(images/mainbg.jpg) left 40px repeat-x;018.}019. 020./* links021.*************************/022. 023.a:link, a:visited, a:hover, a:active024.{025.text-decoration: none;026.}027. 028./* inline elements029.*************************/030. 031.strong032.{033.font-weight: bold;034.}035. 036./* menu-specifc037.*************************/038. 039.#menu040.{041.position: fixed;042.z-index: 5;043.top: 0;044.left: 0;045.width: 100%;046.height: 40px;047.line-height: 40px;048.background: #ffb35a url(images/topbg.gif) repeat-x;049.border-bottom: 1px solid #000;050.}051. 052.#menu ul053.{054.margin: 0 auto;055.}056. 057.#menu ul li.hasmore058.{059.background: url(images/drophighlight.png) no-repeat center 27px;060.}061. 062.#menu ul li h4063.{064.margin: 0;065.}066. 067.#menu ul li h4 a068.{069.font-size: 14px;070.color: #000;071.font-weight: bold;072.padding: 0 15px;073.}074. 075.#menu ul li a076.{077.color: #9b2021;078.padding-left: 4px;079.}080. 081.#menu ul li a img082.{083.vertical-align: middle;084.}085. 086.#menu ul li a:hover087.{088.background: url(images/topselectionleft.png) top left no-repeat;089.}090. 091.#menu ul li a span092.{093.display: block;094.padding: 0 15px 0 11px;095.}096. 097.#menu ul li a:hover span098.{099.background: url(images/topselectionright.png) top right;100.}101. 102.#menu ul.dropdown103.{104.padding: 10px;105.background-image: url(images/dropdown.png);106.overflow:hidden;107.border-bottom: 1px solid #dc902f;108.width: 290px;109.}110. 111.#menu ul.dropdown li a112.{113.border-bottom: 1px solid #e5cd8e;114.line-height: 30px;115.overflow: hidden;116.height: 30px;117.}118. 119.#menu ul.dropdown li.last a120.{121.border-bottom-width: 0;122.}123. 124.#menu ul.dropdown li a:hover125.{126.background: url(images/menuarrow.png) no-repeat left center;127.}128. 129.#menu ul li h4 a:hover130.{131.background-image: none;132.}01.$(function () {02. 03.$('.dropdown').each(function () {04.$(this).parent().eq(0).hoverIntent({05.timeout: 100,06.over: function () {07.var current = $('.dropdown:eq(0)', this);08.current.slideDown(100);09.},10.out: function () {11.var current = $('.dropdown:eq(0)', this);12.current.fadeOut(200);13.}14.});15.});16. 17.$('.dropdown a').hover(function () {18.$(this).stop(true).animate({paddingLeft: '35px'}, {speed: 100, easing: 'easeOutBack'});19.}, function () {20.$(this).stop(true).animate({paddingLeft: '0'}, {speed: 100, easing: 'easeOutBounce'});21.});22. 23.pic1 = new Image(310, 672);24.pic1.src = "images/dropdown.png";25. 26.pic2 = new Image(4, 40);27.pic2.src = "images/dropselectionleft.png";28. 29.pic3 = new Image(394, 40);30.pic3.src = "images/dropselectionright.png";31. 32.});Congratulations! You’ve walked through creating the fanciest menu ever! Here are the files this menu uses:
Source: webdesigndev.com