by Larisa Thomason,
Senior Web Analyst,
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
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.
Here are the CSS rules for the basic menu:
font-weight : bold;
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
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:
<a href="YouAreHere_Example1.htm" id="menu-current">Current
<a href="" id="menu-home">NetMechanic Home Page</a>
<a href="" id="menu-newsletter">Newsletter Archive</a>
<a href="" id="menu-powerPack">Search Engine
<a href="" id="menu-toolbox">HTML Toolbox</a>
<a href="" id="menu-browserPhoto">Browser Photo</a>
Note the ID attribute added to each link.
- Add an ID attribute to the BODY tag of each page referred to in the navigation menu. So we take the example page and modify the BODY tag like this:
If we were adding this to the NetMechanic site, we might change the BODY tags of the individual pages 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.