CSS for Navigation
Eric A. Meyer
Principal, Complex Spiral Consulting
- Seybold/WOW
- San Francisco, CA
- 9 September 2003
- 3:15pm - 4:15pm
- CSS For Navigation
- www.Seybold365.com/sf2003
Tripping the list fantastic
- Navbars, toolbars, sidebars—they all have one thing in common: they're collections of links
- We can represent such a collection as a list and style it
- With this simple structure, we have a lot of presentational flexibility
- Whether the list is ordered or not is up to you; unordered is more likely the sensible choice
Making a sidebar
- By removing the bullets and indentation, we have what looks like break-separated text
- We can get a lot done just by styling the
li
elements
- Separators are a breeze, and you can cap the list using the
ul
element
- It can be a panel, an open list, almost anything you want
Making a navbar
- It's a little trickier, but you can turn a list into a horizontal navbar
- Instead of blocking out links, we need to make the list itself inline
- Now we can visually separate the links with borders
- Watch out for how inline elements behave in different browsers, especially whitespace between them
- With some creative styling, we can even create a "tabbed" interface
Link Highlighting
- Highlighting the current page (or section) can be driven using IDs and classes
- For example, we could class the page while ID'ing the links... or vice versa
- The CSS is a little convoluted, but it beats most alternatives
- Another possibility is just to insert a "current" ID on the appropriate link (a great approach for CMS systems)
Another quick effect
- Let's go back to the panel and add a heading
- By making the heading inline, we end up "shrink-wrapping" the text
- Adding a background color obscures pieces of other elements
- Relative positioning moves the element out of its usual place
- Some changes to backgrounds, borders, and text alignment, and we have a whole new menu look
So what's the catch?
- Maybe none—it depends on your visitor demographics
- NN4.x is not going to respond well to the inline list technique, and IE4 isn't likely to be much happier
- If you hide the styles from old browsers, they'll just render an unordered list
- That could be okay for a sidebar, depending on how you place it on the page
- Weigh the old-browser situation against the accessibility and efficiency wins
The Future?
- It's possible to create popup menus without JavaScript!
- The key here is arbitrary-element hover styling
- Suppress sublists by default, then reveal them on hover
- There are some limitations versus JS-driven menus
In Closing
- Please give us feedback on this session and the event as a whole
- Contact paths:
- Many thanks for your time and attention!