Вы находитесь на странице: 1из 13

Navigation Rollovers With Drop-downs

This post is a continuation of Super Fantastic CSS Navigation Image Rollovers where we created CSS rollovers with a single image, so it would probably be a good idea for you to take a look at that post in order to follow along with this one if you havent done so yet. Were going to take the code from that post, and adapt it to Stu Nicholls CSS drop-downs. He has done a great job in creating a multi-level drop-down menu that is verified to work in most browsers, and without any slow loading javascript.

Sorry there has been such a delay between these two posts, Ive been out sick with a nasty cold for a while. I decided that I can finally breath through my nose well enough to finish this thing.

Here is an example of what well be creating. You can find all of the necessary CSS in the header of that page. Keep in mind, I also use Meyers Reset CSS sheet which takes care of getting rid of the unordered lists natural padding and styling. CSS reset style sheets are great for evening the playing field across multiple browsers, because they all have their own styles that they apply to certain elements and theyre never the same.

Weve already got our image for the navigation rollovers from the previous post, and so now were going to introduce the second level of navigation into our HTML: 01. <div id="navigation"> 02. <ul> 03.

04. <li><a href="index.php" class="link1">Home</a></li> 05. <li><a href="metal" class="drop link2">Metal<!--[if IE 7]><!--></a><!--<![endif]--> 06. <!--[if lte IE 6]><table><tr><td><![endif]--> 07. <ul> 08. <li><a href="gold.php">Gold</a></li> 09. <li><a href="aluminum.php">Aluminum</a></li> 10. <li><a href="brass.php">Brass</a></li> 11.

12. <li><a href="stainless-steel.php">Stainless Steel</a></li> 13. </ul> 14. <!--[if lte IE 6]></td></tr></table></a><![endif]--> 15. </li> 16.

<li><a href="plastic" class="drop link3">Plastic<!--[if IE 7]><!--></a><!--<![endif]--> 17. <!--[if lte IE 6]><table><tr><td><![endif]--> 18. <ul> 19. <li><a href="polyethelene.php">Polyethelene</a></li> 20.

21. <li><a href="polycarbonate.php">Polycarbonate</a></li> 22. <li><a href="fiberglass.php">Fiberglass</a></li> 23. <li><a href="pvc.php">PVC</a></li> 24. </ul> 25. <!--[if lte IE 6]></td></tr></table></a><![endif]--> 26. </li> 27. <li><a href="services.php" class="drop link4">Services<!--[if IE 7]><!--></a><!--<![endif]--> 28.

29.

<!--[if lte IE 6]><table><tr><td><![endif]--> 30. <ul style="height:240px;top:-40px;"> 31. <li><a href="art.php">Art</a></li> 32. <li><a href="design-for-manufacturing.php">Design For Manufacturing</a></li> 33. <li><a href="prototyping.php">Prototyping</a></li> 34. <li><a href="quickturn.php">Quickturn</a></li> 35. <li><a href="safety-design.php">Safety Design</a></li> 36.

37. <li><a href="kits-and-assembly.php">Kits &amp; Assembly</a></li> 38. <li><a href="rfq.php">Request For Quote</a></li> 39.

40. </ul> 41. <!--[if lte IE 6]></td></tr></table></a><![endif]--> 42.

</li> 43. <li><a href="news.php" class="link5">News</a></li> 44.

45. <li><a href="catalog.php" class="link6">Catalog</a></li> 46. <li><a href="about.php" class="link7">About Us</a></li> 47. <li><a href="contact.php" class="link8">Contact</a></li> 48. <li><a href="users.php" class="link9">Registered Users</a></li> 49. </ul> 50. </div>

Were creating 3 dropdowns that come out of the links Metal, Plastic, and Services. We havent made too many changes to our HTML from the previous post. The #navigation ID has been moved off of the ul and onto the containing div because of the way that Stu Nichols did many of his selectors in his drop-down. Also, please take note of the way that the Internet Explorer conditional comments have been structured in this. You dont want to screw those up, or IE will render things ugly the way that it normally does!

That was the easy part, and now for the CSS which I will just break down line for line. Once again, you can get the whole thing at this link.

Most of this code is just styling that will be specific for the navigation for every link , and there are some old IE hacks in here as well. Make sure you just replace the code that you need, and put your width in all of the right places: 01. #navigation {width:145px;position:relative; z-index:100;margin-top:50px; border-right:1px solid #999; padding:10px 0px; float:left;} 02. /* hack to correct IE5.5 faulty box model */ 03. * html #navigation {width:145px; width:144px;} 04. /* float the list to make it horizontal and a relative positon so that you 05. can control the dropdown menu positon */ 06. #navigation li {width:145px;position:relative;} 07. /* style the links for the top level */ 08. #navigation a, #navigation a:visited {display:block; height:47px; 09. background:url(navigation.png) top left; text-indent:-9000px;} 10. /* a hack so that IE5.5 faulty box model is corrected */ 11. * html #navigation a, * html #navigation a:visited {width:145px;width:145px;}

Just follow the CSS comments that Stu Nichols has placed in his code here. Were still not doing any huge alterations other than styling things specific to this drop-down. For our purposes were only doing one sub-level, but I left the CSS in here for you to style a third one if needed: 01. /* style the second level background */ 02. #navigation ul ul a.drop, #navigation ul ul a.drop:visited { 03. background:#2d3d47} 04. /* style the second level hover */ 05. #navigation ul ul a.drop:hover{background:#fff; color:#202c32;} 06. #navigation ul ul :hover &gt; a.drop {background:#fff; color:#202c32;} 07. /* style the third level background */ 08. #navigation ul ul ul a, #navigation ul ul ul a:visited { 09. background:#e2dfa8;} 10. /* style the third level hover */ 11.

#navigation ul ul ul a:hover {background:#b2ab9b;}

This is where you figure out your exact positioning of the sub-levels, as well as style their appearance. Once again, the extra sub-level code is left in here and unaltered, just make sure that you are going down each line and putting in the code that is specific for your design: 01. /* hide the sub levels and give them a positon absolute so that they take up no room */ 02. #navigation ul ul { 03. visibility:hidden; 04. position:absolute; 05. top:-10px; left:145px; 06. padding:4px; 07. background:#2d3d47; 08. height:96px; width:145px; 09. border-left:1px solid #999; 10. } 11.

/* another hack for IE5.5 */ 12. * html #navigation ul ul {top:-9px;top:-10px;} 13.

14. /* position the third level flyout menu */ 15. #navigation ul ul ul{left:149px; top:-1px; width:149px;} 16.

17. /* position the third level flyout menu for a left flyout */ 18. #navigation ul ul ul.left {left:-149px;} 19.

20. /* style the table so that it takes no ppart in the layout - required for IE to work */ 21. #navigation table {position:absolute; top:0; left:0; border-collapse:collapse;} 22.

23. /* style the second level links */ 24.

#navigation ul ul a, #navigation ul ul a:visited { 25. background:#2d3d47; 26. color:#fff; 27. height:auto; 28. line-height:14px; 29. text-indent:0px; 30. display:block; 31. padding:5px; 32. text-decoration:none; 33. font-size:11px; 34. } 35. /* yet another hack for IE5.5 */ 36. * html #navigation ul ul a, * html #navigation ul ul a:visited { 37.

width:145px;width:145px;}

This is where we put in a lot of our code for doing the image rollover. This part is a little tricky, because I found an interesting IE6 quirk that stumped me for a while and I still havent quite been able to make sense out of it. Youll see what Im talking about by the consolidation comment on the first sub-level drop-down. Our background position code from the last post is largely unchanged except for when we have a drop-down on links 2, 3, and 4. We now have the selector #navigation :hover > a.link2, #navigation ul ul :hover > a.link2 which is telling the image rollover to stay in its hover state while the user is browsing through the sub-levels: 01. /* style the top level hover */ 02. #navigation :hover &gt; a, #navigation ul ul :hover &gt; a { 03. background:url(navigation.png) top left;} 04.

05. /* top level image background placements */ 06. #navigation a.link1:hover {background-position:-146px 0px;} 07. #navigation a.link2 {background-position:0px -47px;} 08. /* DO NOT consolodate next 2 lines into one, this breaks IE6 */ 09. #navigation a.link2:hover {background-position:-146px -47px;}

10. #navigation :hover &gt; a.link2, #navigation ul ul :hover &gt; a.link2 { 11. background-position:-146px -47px;} 12. #navigation a.link3 {background-position:0px -94px;} 13. #navigation a.link3:hover{background-position:-146px -94px;} 14. #navigation :hover &gt; a.link3, #navigation ul ul :hover &gt; a.link3 { 15. background-position:-146px -94px;} 16. #navigation a.link4 {background-position:0px -141px;} 17. #navigation a.link4:hover {background-position:-146px -141px;} 18. #navigation :hover &gt; a.link4, #navigation ul ul :hover &gt; a.link4 { 19. background-position:-146px -141px;} 20. #navigation a.link5 {background-position:0px -188px;} 21. #navigation a.link5:hover {background-position:-146px -188px;} 22. #navigation a.link6 {background-position:0px -235px;}

23. #navigation a.link6:hover {background-position:-146px -235px;} 24. #navigation a.link7 {background-position:0px -282px;} 25. #navigation a.link7:hover {background-position:-146px -282px;} 26. #navigation a.link8 {background-position:0px -329px;} 27. #navigation a.link8:hover {background-position:-146px -329px;} 28. #navigation a.link9 {background-position:0px -375px; height:65px;} 29. #navigation a.link9:hover {background-position:-146px -375px;}

The rest of the code is just a little extra styling. We now have a fast drop-down menu with image roll-overs using nothing more than CSS! Again, you can view the code for our final result here. If you have any tips, questions or problems that you run into while trying to reproduce this, please leave a comment.

Advertisement: Choose from a wide array of bracelets available in store for you today. LusterForever jewelers has a wealth of designs that fits your discriminating taste. See for yourself.

Вам также может понравиться