Transforming unsorted list into CSS horizontal menu

19. October 2007 10:42 by rtur.net in CSS  //  Tags: , ,   //   Comments (3)

Lately I’ve been working on the new cool theme for BlogEngine that I’m going to share with community. One of the tasks is to let it be more content oriented, which is a bit different then other themes. Basically, it comes down to having good navigation menu that will allow you to utilize stand-alone pages in BlogEngine to its full potential. Currently, you have a choice to make page a “child” of the other page so that pages do have hierarchy and can be nested into the tree-like structure. It just not rendered by BlogEngine this way, at least not right now. To make it happen I used code published on the BlogEngine forum, just making a few changes to fit my needs. This is a simple control that looks inside core library and renders all BlogEngine pages into unsorted list. Then, you can just add it to your theme and here you have it:

In some cases, that’s enough. You just add style sheet to make list look pretty and you done. This approach has its own advantages; everything is just one click away. On the other hand, it takes lots of space and if you have large content hierarchy that is not an option. You probably need full blown horizontal menu with pull down submenus. Horizontal menus are slim, they do not take much space and users are used to this kind of navigation. So the task is to transform UL to the horizontal menu, as you can see on the left picture.

Then, when user hovers over the top menu, we show sub menus and, if sub menu has children, hovering over it will show sub-sub menus and so on. Normal stuff, every web user saw it and used it and takes for granted. It is possible to achieve this behavior with pure CSS using some voodoo magic, but let’s not go there. Actually, it is easier to do combining CSS with javascript. All that needs to be done – hide all submenus on load and show them when parent node triggers mouse over event. All CSS is doing is lining up items using typical layout tricks. You can see here how it all works.

The entire code for this page is shown below. Not too exiting yet, but ones you get that far - it is all down the hill from here. Next time I’ll cover how to make it look good and run inside BlogEngine (or DotNetNuke – same rules applied).

  1. <html>
  2. <head>
  3. <style type="text/css">
  4. #navmenu li{
  5. white-space:nowrap;
  6. position: relative;
  7. display: inline;
  8. float: left;
  9. margin: 0 15px 0 15px;
  10. }
  11. #navmenu li ul{
  12. position: absolute;
  13. top: 1.1em;
  14. display: block;
  15. list-style-type: none;
  16. padding: 0;
  17. margin: 0;
  18. left: 0;
  19. visibility: hidden;
  20. }
  21. #navmenu li ul li{
  22. display: list-item;
  23. margin: 0;
  24. padding:0;
  25. float: left;
  26. }
  27. </style>
  28. <script type="text/javascript">
  29. function buildsubmenus_horizontal(){
  30. var ultags=document.getElementById("navmenu").getElementsByTagName("ul");
  31. for (var i=0; i<ultags.length; i++){
  32. ultags[i].parentNode.onmouseover=function(){
  33. this.getElementsByTagName("ul")[0].style.visibility="visible"
  34. }
  35. ultags[i].parentNode.onmouseout=function(){
  36. this.getElementsByTagName("ul")[0].style.visibility="hidden"
  37. }
  38. }
  39. }
  40. if (window.addEventListener)
  41. window.addEventListener("load", buildsubmenus_horizontal, false)
  42. else if (window.attachEvent)
  43. window.attachEvent("onload", buildsubmenus_horizontal)
  44. </script>
  45. </head>
  46. <body>
  47. <br>
  48. <ul id="navmenu">
  49. <li><a href="#">Top One</a></li>
  50. <li><a href="#">Top Two</a>
  51. <ul>
  52. <li><a href="#">Sub One</a></li>
  53. <li><a href="#">Sub Two</a></li>
  54. </ul>
  55. </li>
  56. <li><a href="#">Top Three</a></li>
  57. </ul>
  58. </body>
  59. </html>

 

Comments (3) -

sezer
sezer
5/2/2008 3:51:47 PM #

CSS "Cascading Style Sheets" LessoNs - WeB DesigN LessoN - - Web site : http://WWW.css-lessons.ucoz.com/index.html


Saeed
Saeed
10/12/2008 12:54:33 PM #

Thanks for sharing this useful information.
Good tutorials in CSS and Menus.

Mountain Tour Sports
Mountain Tour Sports
3/24/2009 12:32:20 AM #

Thank you for giving me pleasure only by reading your blog webmaster.. Just want you to know that I really appreciate your post and it really gives useful information. Thanks again!

Comments are closed

Recent Tweets

Twitter May 21, 20:23
Had to unplug Kinect to watch Xbox one video on 360. Poor thing freaked out by people screaming commands and stopped playing every few secs.

Twitter May 21, 18:38
Wonder how long it takes for http://t.co/O1XAGcMV5l web gallery to test app. Stuck in "testing" state for a few days now. Is this manual?

Twitter May 15, 19:37
Trying to figure out if Linux Mint need an upgrade. And I thought Windows versions complicated. Is that LMDE thing what I need for my Nadia?

@rtur