|
|||||||
Styling Menus as Lists with CSSUsing CSS and Unordered Lists is an incredibly powerful way of presenting certain types of content. In this article I'll show you how to style a menu using an unordered list and a little CSS. A word about the examplesIn order to display the examples as they evolve it's necessary to use different classes for each example. If you want to take the final CSS and develop other list styles from it then you will only need the final stylesheet, for the class "list". As we develop the examples, I'll show the CSS with the new additions and changes highlighted in bold so you can see the effect of each change. Finally, these examples work best in CSS2-compliant browsers such as Firefox. They should look reasonably OK in non-compliant browsers such as Internet Explorer (eventually, at least) but it would take a lot more work to make them look identical in all browsers. Cross-browser compatibility for CSS2 when browsers such as Internet Explorer have such a poor implementation of CSS2 is far beyond the scope of a single article. The ListFirst, let's have a look at how a list is structured. Here's a very simple list defined in HTML:
With no styling, this will appear as follows: Ugh!Not terribly impressive, and so "Web 1.0". But the good news is that using CSS we can make lists look a lot nicer. Let's get rid of the bullets. We'll also put a border around the UL so we can see what we're doing. We'll remove the border when we're finished. We use this CSS:
and give our list a class of list-1, vis:
which gives us this: Fixing the widthOk, we can see from the border that we need to do something abut the width of the UL. While we're at it let's get rid of the underlines on the Anchor tags.
Remember, we're only changing the class so the examples work on a single page. When you apply these techniques to your own menus you'll only need one class. Here's the change to the class.
And the list as it appears now: Now, notice when you hover the mouse over the links, they change colour (they are actually picking up the default Website Repairs stylesheet). Unstyled they'd also show a different colour when they have been visited. HoverLet's fix the colour on Hover, and while we're at it we'll add a bit of padding around the Anchors. Note: I'll only show the CSS from now on unless the HTML changes; I'm assuming you realise that the UL's class changes each time to match the next example along.
This now renders as follows: BackgroundLet's add a background colour.
Here's what our a list of class list-4 looks like: UL PaddingOh, dear. Now that we've styled the Anchors to have a blue background, we see that the UL is adding some padding to the left hand side. Let's get rid of that, and also while we're at it let's add a border to the left of the Anchors.
Here's how it looks now: Finishing UpOk, let's be daring and add a few things at once. We'll make the links and their left-hand border change colour when we hover over them, add some spacing between the list items, and let's get rid of the border around the List since we don't need it any more.
Internet ExplorerIf you are reading this page in one of the versions of Internet Explorer, you are probably wondering what's going on. Everything from list-3 onwards has far too much vertical spacing between the items. For those viewing in a proper browser such as Firefox, the image below shows what Internet Explorer users are seeing at this stage.
This isn't a fault of the CSS, it's a problem with Internet Explorer's implementation of CSS. No matter, let's fix things so that even Microsoft's brain-dead browser can cope with it. Although it shouldn't be necessary, if we set the width of the Anchor tags to 100%, the problem goes away.
Here's the final list, in a form that even IE can cope with. FinallyThis has been a very quick overview of *some* of the things you can do with CSS and Unordered Lists to quickly build easy to configure menus, but we've only scratched the surface. Now that you have the basics, browse on over to Listamatic where there are more CSS/UL menus than you can shake a stick at. I know you'll be impressed.
|
|||||||