Join James Williamson for an in-depth discussion in this video Organizing menus with lists, part of CSS: Styling Navigation.
Before we begin working with CSS, I want to take some time to look at structuring site navigation properly. HTML and CSS work hand in hand, and you want to make sure that you start with clean, well-structured HTML. We'll start that by structuring a menu. Now to do that, I have the menu.htm file open from the 01_01 folder, and as you can see, there is not a lot going on, on the page here. We just have a series of links inside the body tag one after the other, Home, Products, Blog, About, Contact.
If these were scattered all the way throughout the site, obviously they wouldn't be related to each other at all, but when they are in this order here, when they are one right after the other, people will begin to infer a specific meaning about them. Let me just show you what I mean by that by switching over to looking at this page within a Browser. So here up at the top of the page, now we can see Home, Products, Blog, About, Contact. Most people are going to assume--and correctly in this case--that this is a menu. Now, menus are related groups of links. They mean something to each other, in this case, it would be our site's navigation of the Home page, Products page, Blog, About, Contact, so sort of the main pages of this particular small little site.
The problem here isn't so much the visual aspect of this. We could style this any way that we wanted to right now. We could take these links, and we could apply background colors to them and space them apart and create rollovers, and it would look just like perfect site navigation. Now, the problem here right now lays in the structure of the HTML. If I go back to the code, you can see that there is nothing here really telling any type of user agent that these links are related to each other at all. To do that, we need to add a little bit of semantic structuring to these links that basically groups them together. So we have a couple of choices here.
I mean, we can certainly wrap them inside of a paragraph, we can wrap them inside of a div tag, but those are limited in terms of their structural ability. They basically just segment the links as belonging to a certain section of content. They don't really say that these links relate to each other in any definable way. The best way to do that is to use a list. Lists are perfect for menus. As we mentioned before, menus are really simply a list of related links. So it would make sense to use a list to represent that. So, what I am going to do is right after the opening body tag, I am going to go ahead and create an unordered list tag, and then at the end of these links, I will close that out.
And then for each one of these anchor elements, I am going to go ahead and wrap them in a list item tag. You're going to want an opening list item and then a closing list item that's going to surround each one of these anchor links. Now, in doing this, we're basically semantically saying, or structurally saying that these links are related to each other. We're not really passing any additional information about how these links relate to each other right now, but at least we're defining the fact that these links belong together, they relate together, and they are part of a list.
So if I save this and go ahead and preview that into my Browser, I can see that there is a pretty big change here. Actually, this throws up a little bit of a roadblock to us stylistically, because now we're going to have to make basically these links look so that they are not part of a list when we structure our site menu. However, structurally, they are exactly what we need. And since we have more control over our presentation than we do the page structure, you want to make sure that the page structure is correct. We used an unordered list here, but that's not always going to be the case.
Now, I would say it is certainly the most widely- used list type in terms of representing menus, but there are times when an ordered list would be appropriate. Let's say that you had a menu that describes the steps for doing something, maybe that you have links to individual steps within a recipe, maybe you have links to individual steps of registering for a website. Well, those would be represented better by an ordered list so that if the user agent doesn't support styles, there is a distinct order to those links that you want to pass along to the user. In this case, there really isn't a distinct order to the links.
If we were to shuffle these around, it really wouldn't harm the menu at all, so an unordered list is certainly appropriate. One more thing about structuring menus with lists, not only do we get this really nice semantic structure, it also gives us multiple styling hooks that we can use. For example, here we have the unordered list tags, and then we have all these individual list item elements that we can style as well. Now, that gives us a distinct advantage when it comes time to style our menus.
- Organizing menus with lists
- Creating block-level links
- Styling links, link states, and image links
- Defining link dimensions
- Controlling link spacing in a menu
- Creating rollovers
- Clearing floats
- Indicating current pages
- Controlling cursor states
- Building dropdown menus
- Creating CSS-only buttons