Navigate
Sponsors
|
A Menu Rollover Effect with CSSIntroductionThis guide shows how to create a stylish rollover effect for a menu which might normally be done with Javascript or DHTML. CSS is used to do the menu highlighting. Tested in We have created 2 different versions of this tutorial so you can choose which one suits your application best. (first is best).
Pssst...we've also built a guide on how to add a transition effect to the menu using CSS filters. It's IE only but the menu will appear as normal in other browsers anyway. I want the transition effect... In practice both methods operates much the same way. Using lists requires the least amount of HTML (some of us hand-coders are lazy) and it's also the method I recommend. Note: traditionally HTML lists are unpredictable and awkward buggers but thanks to CSS we can easily take control of the lists to achieve the effect we need. Step 1.First create a containing div which will hold our menu, simply:
<div id="list-menu"> At this stage we only need to define the width (positioning also if you like):
#list-menu { Step 2.Next we'll just create our test links in an unordered html list:
<ul> Step 3.Now we have to take control of the list by removing the indents. We also need to remove the default bullets (or numbers if you decided to use an ordered list)
#list-menu ul { Setting the margin and padding to zero removed the indents from IE/Opera and Netscape/Mozilla respectively. At this stage we can also define text styles by adding the following to the above CSS:
font-family: verdana, arial, sanf-serif; We should also create a little bit of space between the list items to improve presentation, so we create the following css for each list item:
#list-menu li { This will place a margin of 2px above each list item. Note: At this stage we should set the font styles in the container (<div id="list-menu">) otherwise we may lose control of the margins in Internet Explorer. [Thanks to Alex Schleifer!] The result of Step 3 is the following: Step 4.Now we just need to create styles for the actual links.
#list-menu a { An important point to mention at this stage is the width setting. When we tested this, leaving out the width setting caused the menu to mess up in IE6. This width combined with the side padding should equal the width we set for the container div in Step 1. 120px + (2px + 10px) = 132px This gives us the following: Step 5.The final step is to create the rollover-effect. Like any link rollover effect this is done using the pseudo classes which allow us to define the visited, active and hover states for the links:
#list-menu a:link, #list-menu a:active, #list-menu a:visited { So we now have the following: For extra effect we could add a background image on hover:
#list-menu a:hover { Which results in the following: The background image I use here is the following: As a last point, adding onfocus="this.blur()" into each link code will get rid of that annoying dotted line which appears when the link is clicked. That's it. Now it's up to you to customise and play with. I want the transition effect... |