Readable text and understandable navigation are the two most important components of site usability. One easy way to make your navigation menu more user-friendly is to disable the current page in the menu. We'll show you how to accomplish this using basic HTML and CSS: no JavaScript or PHP coding required!
Control Display By Styling Link States
Very young human infants seem to have three states of being: sleeping, eating, and crying. Hyperlinks in your navigation system have four states of being — and luckily are quieter and easier to manage:
Visited: indicates a page the visitor has already seen and who's URL is in the browser's history file.
Focus: the link is selected by tabbing with the keyboard instead of with a mouse. Hitting the ENTER key activates the link.
Hover: the link is selected by moving the mouse over the link. Double-clicking the mouse (usually) activates the link.
Active: the page that's currently open in the browser window.
These four states are actually something called pseudo-classes in CSS. You can use them to style your navigation menu based on the link state. Notice something missing in this? There's nothing to indicate which page is currently active, meaning the page currently open in the visitor's browser.
It's not at all uncommon for a user to click a link and have it reload the current page. That's not bad, but it can be a bit aggravating to some users. Fortunately, it's simple to use an additional HTML attribute to highlight the current page � without using any complex coding.
Styling Using A CSS Menu
In a previous Webmaster Tip, Create Stylish Menus, we described how to create a colorful, text-based navigation menu that looks and acts like one creating using images and JavaScript rollovers.
Please refer to that article for a complete description of the technique. For this article, we're going to use the same basic method, but add a twist with the ID element.
That creates a menu like the one on this example page. Note the rollover effect you get when you move your mouse over the links or tab through them
The link for the current page looks and acts the same as the links for the other pages. Now, this link is helpfully titled "Current Page" but most Web sites aren't that clear about which is the active link and which links take you to different pages!
Use ID To Highlight The Current Page
Now, it's easy to find some free JavaScript, PHP, ASP, etc. code snippets that modify the look of the current page in a navigation menu. But it's not always that easy to modify the code to meet your requirements and it's almost impossible to debug the result if you didn't write the code in the first place!
So let's do it the easy way - with CSS and HTML.
We're going to use the ID attribute to link individual pages in the menu to the menu system itself. This is a three-step process:
Add an ID attribute to each link in your menu system, like this:
See how the IDs match? We used the same basic ID name, but added a short identifier (menu) to differentiate them in the CSS rules.
Add a CSS rule that styles whichever page is the current one AND ties together the ID attributes for the page and the link. Yes, this sounds awfully complicated, but see how easy it really is! Here's the new style rule. Just add it before the closing STYLE tag:
Here, we're tying the ID in each page's BODY section to the menu link for that page. If they match, the browser applies the style rules specified here instead of the style rules we set up for the basic menu system.
See how it looks here in this second example page. Look at the page's source code to see all the code working together.
Good For Usability And Accessibility
Like most accessibility techniques, this increases the usability level of the menu for all users. As Web usability expert Jakob Neilsen notes in his book, Designing Web Usability, Internet newcomers don't have nearly the expertise of early adopters:
"People vary in their spatial reasoning skills and in their short-term memory capacity. Programmers and graphic designers tend to get uncommonly high scores on tests of spatial reasoning skills and are therefore good at visualizing the structure of website. Similarly, young people (including most Web designers) certainly have better memories for obscure codes, such as URLs, than older people. It is safe to assume that most users will have significantly greater difficulty navigating a website than its designer have."
Design a navigation system that's easy to use and understand and you'll have a happier set of users who are more comfortable remaining at your site for a long period.
And don't forget: the absolute most important function of a link is to take the visitor to an active page
- not an error message! Check your links on a regular basis with the Link Check Tool that's part of HTML Toolbox. You'll feel secure knowing that you have a working navigation system, working internal and external links, and error-free HTML code.