CSS: Building Navigations From Unordered Lists

Goog Feed, CSS, Design    Comments (8)


So, you have an unordered list that you want to look pretty, huh? Well, I ran into this problem here at work a little while back, and it was requested I share the solution with all.

Problem:

We were (and still are) building a web app, where the navigation would be dynamic based on the user and their permissions, and each nav item could have children under it, sub-items. So we needed to come up with a CSS solution to the problem with a tad bit of JavaScript to make IE happy.

Now, we used a recursive function to query the database for all of the nav items for a user with their children, and possibly their children's children and so on, and when we looped through this result set with ColdFusion, here was a look alike of the nav html that was generated.

<div id="nav2">
<ul id="nav2">
<li><a href="#">Home</a></li>
<li class="sectionparent"><a href="#" class="sectionparent">About</a>
<ul>
<li><a href="#">History</a></li>
<li class="sectionparent"><a href="#" class="sectionparent">Team</a>
      <ul>
       <li><a href="#">First Team</a></li>
       <li class="sectionparent"><a href="#" class="sectionparent">Second Team</a>
         <ul>
          <li><a href="#">Joe</a></li>
          <li><a href="#">Doug</a></li>
          <li><a href="#">Bill</a></li>
         </ul>
       </li>
       <li><a href="#">Third Team</a></li>
      </ul>
    </li>
<li><a href="#">Offices</a></li>
</ul>
</li>
<li class="sectionparent"><a href="#" class="sectionparent">Services</a>
<ul>
<li><a href="#">First Service</a></li>
<li><a href="#">Second Service</a></li>
<li><a href="#">Third Service</a></li>
<li><a href="#">Fourth Service</a></li>
</ul>
</li>
<li class="sectionparent"><a href="#" class="sectionparent">Contact Us</a>
<ul>
<li><a href="#">First Location</a></li>
<li><a href="#">Second Location</a></li>
<li><a href="#">Third Location</a></li>
<li><a href="#">Fourth Location</a></li>
</ul>
</li>
</ul>
</div>

Which looks like this displayed:

Solution:

So what I did was go out on the net and try to find something to fix this problem, and found this solution.

First of all, you will notice that the unordered list has an ID as well as the list item and anchor tag having a class where there are children. With that accomplished, now we just need two more things. Number one some CSS to make it look pretty and secondly a behavior file for IE.

CSS:

body{
   behavior: url(csshover.htc); /* For IE */
}

#nav ul, #nav ul ul {
   margin: 0px;
   padding: 0px;
   width: 150px; /* Width of Menu Items */
   border-bottom: 1px solid #FD5095;
   background: #FC1571;
   font-size: 12px;
   }

#nav ul li {
   font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif;
   font-size:12px;
   font-weight:normal;
   position: relative;
   list-style: none;
   padding: 0px;
   }

#nav ul li a {
   display: block;
   text-indent: 5px;
   text-decoration: none;
   color:#ffffff;
   padding: 5px 0px 5px 0px;
   border: 0px;
   border-top: 1px solid #FD5095;
   zborder-left: 18px solid #FD5095;
   width: 150px;
   }

#nav ul li a:hover {
   zborder-left: 18px solid #FD5095;
   background-color: #FD73AA;
   }
   
#nav ul ul {
   position: absolute;
   display: none;
   left: 150px;
   top: 0px;
   background:#FC1571;
   }

#nav ul li ul li a {
   padding: 5px 0px 5px 0px;
   }

/* Sub Menu Styles */

#nav ul li:hover ul ul, #nav ul li:hover ul ul ul, #nav ul li li li:hover ul {
   display: none; /* Hide sub-menus initially */
   }

#nav ul li:hover ul, #nav ul li li:hover ul, #nav ul li li li:hover ul {
   display: block;
   }

#nav ul li a.sectionparent{
   background: transparent url(navArrow.gif) right center no-repeat;
   }

#nav ul li a.sectionparent:hover{
   background: #FD73AA url(navArrow.gif) right center no-repeat;
   }

#nav ul li a:hover{
   color: #ffffff;
   }

/* Fix IE. Hide from IE Mac */

* html #nav ul li {
   float: left;
   height: 1%;
   }
   
* html #nav ul li a {
   height: 1%;
   }

/* End Fix */

Behavior File:

Click here to download the htc behavior file.

Final Result:

Click Here To See A Live Demo!

This has been very useful for me, I hope it helps you as well!

Related Blog Entries

Comments
Where be csshover.htc?
# Posted By Phillip Senn | 9/2/06 11:48 AM
Never mind.
You see the link that says
"Click here to download the htc behavior file"?

Well, that's it!
D'oh!
# Posted By Phillip Senn | 9/2/06 11:54 AM
Sometimes you say:
background:
when you mean
background-color:
# Posted By Phillip Senn | 9/2/06 1:03 PM
As I move around in the list, my IE 6 is constantly aborting.
# Posted By Phillip Senn | 9/2/06 1:09 PM
@ Phillip Senn - Hey Phillip, check the path from your CSS file to the HTC file and make sure that is correct.

Joe
# Posted By Joe Gautreau | 9/4/06 1:46 PM
Hi Joe, why bother with Javascript to make it all work (especially such an old and dangerous variant as HTC) when you can do what you do without Javascript? Please go to http://cssplay.co.uk and see how the magic unfolds, cross-browser guaranteed ;-)
# Posted By Sebastiaan | 11/12/07 5:31 AM
Wow Sebastian! This is great! http://www.cssplay.co.uk/menus/ is a gigantic page of css menus! Where to begin?
# Posted By Phillip Senn | 11/12/07 10:31 AM
Hi I need your help i really want to create my own website/web page but i don't know how to go about doing it so can you please help me out?
# Posted By Oyun Siteleri | 8/26/08 5:51 PM