JavaScript
  Home arrow JavaScript arrow Page 2 - Building a Dynamic Menu with CSS and JavaS...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Building a Dynamic Menu with CSS and JavaScript, part 1
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 42
    2005-06-22

    Table of Contents:
  • Building a Dynamic Menu with CSS and JavaScript, part 1
  • Coding the menu: CSS and HTML into action
  • The dynamic factor
  • The "menu" JavaScript object

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building a Dynamic Menu with CSS and JavaScript, part 1 - Coding the menu: CSS and HTML into action


    (Page 2 of 4 )

     

    Let’s see the underlying code behind the sample menu. First, here are the CSS declarations: 

    <style type="text/css">

    a.regbutton:link,a.regbutton:visited {

        float: left;

        display: block;

        font: normal 11px "Verdana", Arial, Helvetica, sans-serif;

        color: #000;

        text-align: center;

        text-decoration: none;

        background: #fff url("bg.gif") repeat-x center left;

        width: 80px;

        padding: 10px 0px 9px 10px;

    }

    a.regbutton:hover {

        float: left;

        display: block;

        font: normal 11px "Verdana", Arial, Helvetica, sans-serif;

        color: #fff;

        text-align: center;

        text-decoration: none;

        background: #fff url("bg.gif") repeat-x center left;

        width: 80px;

        padding: 10px 0px 9px 10px;

    }

    .leftcorner {

        float: left;

        background: #fff url("lc.gif") no-repeat center center;

        width: 10px;

        padding-top: 8px;

        padding-bottom: 5px;

    }

    .rightcorner {

        float: left;

        background: #fff url("rc.gif") no-repeat center center;

        width: 10px;

        padding-top: 8px;

        padding-bottom: 5px;

    }

    #dynmenu {

        padding: 0px;

    }

    </style>

    That’s a quite lengthy CSS listing to be analyzed. In fact, it’s not complicated at all, as you’ll see in a moment. I’ve just defined the classes for styling each link present in the menu, as well as the left and right corners respectively. Doing so, I’ve created the "regbutton" class, to give a button appearance to each one of the links. Please note that I’ve specified the tiny "bg.gif" background image to be horizontally tiled across the button itself, achieving the consistent look.

    Then, I’ve declared the "leftcorner" and "rightcorner" classes, obviously for displaying the left and right menu corners. Finally, the "dynmenu" selector is defined, for styling the main menu container within the markup. I'm sure you’ll agree that all of this code is pretty self-explanatory. 

    Now, it’s time for the HTML markup listing: 

    <div id="dynmenu">

    <span class="leftcorner">&nbsp;</span>

    <a href="#" class="regbutton" title="About Us">About Us</a>

    <a href="#" class="regbutton" title="Products">Products</a>

    <a href="#" class="regbutton" title="Services">Services</a>

    <a href="#" class="regbutton" title="Links">Links</a>

    <a href="#" class="regbutton" title="Contact">Contact</a>

    <span class="rightcorner">&nbsp;</span>

    </div>

    As you can see, the above HTML is very clear. The only tricky parts correspond to the left and right corners. I’ve used the powerful and versatile <span> tag to create the corner effects, with no structural value. There are lots of different ways to achieve this kind of visual effect, using <div> elements, but I’ve decided to keep it that way for simplicity. 

    Let’s congratulate ourselves! With a few lines of code, we have a fully functional navigation menu that will work in most browsers. Here’s the full code for the recently developed static menu: 

    <html>

    <head>

    <title>STATIC MENU</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <style type="text/css">

    a.regbutton:link,a.regbutton:visited {

        float: left;

        display: block;

        font: normal 11px "Verdana", Arial, Helvetica, sans-serif;

        color: #000;

        text-align: center;

        text-decoration: none;

        background: #fff url("bg.gif") repeat-x center left;

        width: 80px;

        padding: 10px 0px 9px 10px;

    }

    a.regbutton:hover {

        float: left;

        display: block;

        font: normal 11px "Verdana", Arial, Helvetica, sans-serif;

        color: #fff;

        text-align: center;

        text-decoration: none;

        background: #fff url("bg.gif") repeat-x center left;

        width: 80px;

        padding: 10px 0px 9px 10px;

    }

    .leftcorner {

        float: left;

        background: #fff url("lc.gif") no-repeat center center;

        width: 10px;

        padding-top: 8px;

        padding-bottom: 5px;

    }

    .rightcorner {

        float: left;

        background: #fff url("rc.gif") no-repeat center center;

        width: 10px;

        padding-top: 8px;

        padding-bottom: 5px;

    }

    #dynmenu {

        padding: 0px;

    }

    </style>

    </head>

    <body>

    <div id="dynmenu">

    <span class="leftcorner">&nbsp;</span>

    <a href="#" class="regbutton" title="About Us">About Us</a>

    <a href="#" class="regbutton" title="Products">Products</a>

    <a href="#" class="regbutton" title="Services">Services</a>

    <a href="#" class="regbutton" title="Links">Links</a>

    <a href="#" class="regbutton" title="Contact">Contact</a>

    <span class="rightcorner">&nbsp;</span>

    </div>

    </body>

    </html>

    Note that I’ve coded some dead links for the menu. This seems to be a little obvious, but if you’re going to use the example, don’t forget to replace them with the corresponding URL values for your site. 

    So far, so good, the menu is working and everything is fine. But did I mention the word "dynamic” in the above lines? Where is the dynamic menu? Keep reading. 

    More JavaScript Articles
    More By Alejandro Gervasio


       · The article shows how to define a simple dynamic navigational menu going from a...
       · Hello Sir,First of all congrates that u built this useful menu .I created this...
       · Thank you for the kind comments on my article. With reference to your question, the...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT