CSS Navigation Elements

When you are creating a CSS/HTML page, you will definitely want navigational elements in that web page. The most common way to create navigational elements is through CSS styled lists. But you should remember one simple thing – your navigational elements should contain display: block.

The display: block property is very essential in CSS navigation because navigation buttons and areas are absolutely meant to be like buttons or blocks of the web page. In case you leave the links in your navigation as display: inline; (default for elements), then only the text area in your navigation will become clickable.

To get the best results, add horizontal navigation menu and vertical navigation menu that will help your pages to become more accessible while remaining compliant to standards.

Horizontal Navigation Menu

To create horizontal menu, start with the outside and work in. if you want your navigation to start in left corner, you need to set it with 0 left margin and padding, an float it to the left.


ul#navigation {
    float: left
    margin: 0;
    padding: 0;
    width: 100%;
    background-color: #039;
}

But the main magic in horizontal navigation menu lies in its list items. List items are normally block elements, which means that they will have new line placed before and after each one. By switching the display from block to inline, you can force the list elements to queue up next to one another.


ul#navigation li {display: inline;}

A top border can also be added to delineate the buttons when they are hovered over:


ul#navigation li a {
    display: block;
    text-decoration: none;
    padding: .25em lem;
    border-bottom: solid 1px  #39f;
    border-top: solid 1px #39f;
    border-right: solid 1px #39f;
}

a:link, a:visited {color: #fff;}

ul#navigation li a:hover {
    background-color: #fff;
    color: #000;
}

Vertical Navigation Menu

It is same as creating the horizontal navigation menu. The first move in such creation is to define the width of the menu. This will ensure that even if the menu items are long, they will not push the rest of the layout over or cause horizontal scrolling.


ul#navigation {width: 12em;}

Once the width is set, other things like list-style [get rid off the bullets], borders, text alignment, margins and background colors.


ul#navigation li {
    list-style: none;
    background-color: #039;
    border-top: solid 1px #039;
    text-align: left;
    margin: 0;
}

Once the basics for the list items are set, you can start playing with how the menu looks like in the links area. Firstly, you should style the ul#navigation li a, then the a:link, a:visited, a:active, and a:hover (if you want them). For the a:links, let it be made block element rather than default in-line. This forces them to take up the entire space of the li – and they act more like a paragraph, which makes them easier to style as menu buttons. The other best things that can be done are removing the underline (text0-decoration: none) as this makes the buttons look more like buttons.

Following are the codes:


ul#navigation li a {
    display: block;
    text-decoration: none;
    padding: .25em;
    border-bottom: solid 1px #39f;
    border-right: solid 1px #39f;
}

With the display block, you can set on the a:links, and the entire box of the menu item becomes clickable, and not just the letters. Make sure that you set the link colors (link, visited, active and hover), if you want them to be different than the default blue, purple and red.


a:link, a:visited {color: #fff;}
a:hover, a:active {color: #000;}

To pay some extra attention to the hover state, do the following:


a:hover {background-color: #fff;}

CSS Navigation Techniques Explained

A host of CSS navigation techniques like vertical menus, JavaScript powered menus and tab menus, make navigation menus one of the most important elements for web design.

Here is a list of techniques to create CSS navigation menus:

Drop-down menus, horizontal style

The most important part of creating our menu is menu structure itself. The best way to do this is to build an unordered list, with each sub-menu appearing as a list within its parent list item.

<ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a>
        <ul>
            <li><a href="#">History</a></li>
            <li><a href="#">Team</a></li>
            <li><a href="#">Offices</a></li>
        </ul>
    </li>
    <li><a href="#">Services</a>
        <ul>
            <li><a href="#">Web Design</a></li>
            <li><a href="#">Internet
            <li>Marketing</a></li>
            <li><a href="#">Hosting</a></li>
            <li><a href="#">Domain Names</a></li>
            <li><a href="#">Broadband</a></li>
        </ul>
    </li>
    <li><a href="#">Contact Us</a>
        <ul>
            <li><a href="#">United Kingdom</a></li>
            <li><a href="#">France</a></li>
            <li><a href="#">USA</a></li>
            <li><a href="#">Australia</a></li>
        </ul>
    </li>
</ul>

CSS Tabs Technique

Here are some examples to teach you at how to use CSS to build tabs. These are tabs where the user can click on to view different content within the same space. The tabs comprise simple A tags that use style classes designed particularly to make them look like little tabs found on file folders.

The CSS and HTML code is shown below. Each of the tab is basically a small box with a border on three sides.

div.tabArea {
    font-size: 80%;
    font-weight: bold;
}

a.tab {
    background-color: #f0f0f0;
    border: 1px solid #000000;
    border-bottom-width: 0px;
    padding: 2px 1em 2px 1em;
    text-decoration: none;
}

a.tab, a.tab:visited {
    color: #808080;
}
a.tab:hover {
  background-color: #d0d0d0;
  color: #606060;
}

<div class="tabArea">
    <a class="tab">Tab A</a>
    <a class="tab">Tab B</a>
    <a class="tab">Tab C</a>
    <a class="tab">Tab D</a>
</div>

While the outer DIV element provides a container for the tab, it is basically the “tabArea” class that sets a base font for them. The “tab” class creates a boxed look for the individual links while a :hover pseudo-class is used to highlight the tabs on mouse over.

Active tab

To make one tab stand out, we have to define a new style class names “activeTab” which can be combined with the “tab” class on any link.

a.tab.activeTab, a.tab.activeTab:hover, a.tab.activeTab:visited {
    background-color: #c0c0c0;
    color: #000000;
}

Then the HTML code is updated to make one tab appear active by adding this class name to the link tag.

You can assign multiple style classes to an element by separating the names with spaces in its CLASS attribute.

a.tab.activeTab, a.tab.activeTab:hover, a.tab.activeTab:visited {
    background-color: #c0c0c0;
    color: #000000;
}

Hybrid CSS Dropdowns Technique

Hybrid CSS dropdown technique is a bulletproof way to ensure browser compatibility and maintain usability even for people who have old browsers or difficulty in accessing dropdown menus, either because of a disability or a low level of comfort with the dropdown paradigm.

Firstly, we need to attach an ID to the <ul> element and classes of “off” and “on” to the main list items.

<ul id="nav">
    <li class="off"><a href="#">Renaissance</a>
        <ul>
            <li><a href="#">Brunelleschi</a></li>
            <li><a href="#">Alberti</a></li>
            <li><a href="#">Palladio</a></li>
            <li><a href="#">Michelangelo</a></li>
            <li><a href="#">Bramante</a></li>
        </ul>
    </li>
    <li class="off"><a href="#">Art Nouveau</a>
        <ul>
            <li><a href="#">Mackintosh</a></li>
            <li><a href="#">Guimard</a></li>
            <li><a href="#">Horta</a></li>
            <li><a href="#">van de Velde</a></li>
        </ul>
    </li>
    <li class="on"><a href="#">Modern</a>
        <ul>
            <li><a href="#">Sullivan</a></li>
            <li><a href="#">Le Corbusier</a></li>
            <li><a href="#">Mies</a></li>
            <li><a href="#">Gropius</a></li>
            <li><a href="#">Yamasaki</a></li>
        </ul>
    </li>
    <li class="off"><a href="#">Postmodern</a>
        <ul>
            <li><a href="#">Venturi</a></li>
            <li><a href="#">Eisenman</a></li>
            <li><a href="#">Stern</a></li>
            <li><a href="#">Graves</a></li>
            <li><a href="#">Gehry</a></li>
        </ul>
    </li>
    <li class="off"><a href="#">Digital</a>
        <ul>
            <li><a href="#">Xenakis</a></li>
            <li><a href="#">Lynn</a></li>
            <li><a href="#">Diller+Scofidio</a></li>
            <li><a href="#">Zellner</a></li>
            <li><a href="#">Hadid</a></li>
        </ul>
    </li>
</ul>

These CSS navigation techniques are graphically rich versions that should be taken to the real world.

Codes for CSS Navigation menu

The power of CSS navigation menus on websites remains unparallel. You need to create the right set of codes for CSS and your website will be a fascinating site for all.

HTML code for CSS Menu

To begin with, you need to know the HTML code for CSS menu:

<div id= "nav-menu">
    <ul>
        <li><a href= "#">Services</a></li>
        <li><a href= "#">About Us</a></li>
        <li><a href= "#">Contact Us</a></li>
    </ul>
</div>

That is it! Too simple!

Creating the Boxes

Once the menu items are all lined up against one another, you can take a good look at it. So the CSS rule that rules here is:

#nav-menu li a {
    background: url("background.gif") #fff bottom left repeat-x;
    height: 2em;
    line-height: 2em;
    float: left;
    width: 9em;
    display: block;
    border: 0.1em solid #dcdce9;
    color: #0d2474;
    text-decoration: none;
    text-align: center;
}

The background CSS command is most important. In it, we will specify the URL of the background image for a white background behind this image, for the image to be in the bottom-left of the box and for it to be repeated along the x-axis (i.e. horizontally). If we had not specified the background image to be in the bottom of the box, it would be placed in the top left corner. This would cause the white background color to be visible right below the background image, and not above like it is now.

Removing bullets

You can remove the bullets through a set of new CSS rule:

#nav-menu ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

the important CSS command is the first one, list-style: none, which says that we do not want any kind of bullet in our navigation menu. The others are also so important that they ensure maximum control over the layout and to make sure the text displays in the center of the box.

No wrapping

There is one final CSS rule that needs to be created and assigned with a width to CSS menu navigation. If this rule is not assigned, menu items on the right may be pushed below the others if users resize their screens. The new CSS command is as follows:y

#nav-menu {
    width: 30em
}

That was pretty lucid.

Follow it to give a new dimension to your CSS menus.

Of course there a lot complex ways to create CSS menus, you can even create CSS navigation with sub menus, and sub sub menus, but this is a good start for beginner.

  Subscribe to our RSS Feed   Follow us on Twitter

You've arrived on the "Advanced CSS/HTML Explained" weblog. Here you will find how can you tackle the more difficult topics of Cascading Style Sheets: Floating theory, Forms, Tables, Navigation elements, CSS Positioning. Also you will find lots of Resources, Tutorials, Articles, and Tips, Tricks Tools, and Techniques on CSS.

Recent Posts

Categories

Twitter Updates

Bookmark and Share