Viewers: in countries Watching now:
Join James Williamson, as he shows you how to create elegant menus, links, and buttons that help visitors navigate your site faster and more intuitively. The course covers creating structured navigation that is accessible and clean, styling links, and building horizontal and vertical menus with rollover effects. The last chapter reveals how to create stylish buttons with special effects and CSS sprites.
In this chapter, we'll explore creating our first menu. We'll start with vertical menus, as they require the least amount of formatting and they're a great place to start when learning how to style menus for the first time. To give you an idea of what we're going to be building, here's our finished vertical menu. We have nice, evenly-spaced menu items that respond to mouse-overs or user focus. Like most menus, our vertical menu is simply an unordered list of links. Before we can actually concentrate on the menu styling itself, we'll need to strip out the browser's default list styling.
And to do that, we're going to start with the vertical.htm file found in the 03_01 directory. And if I scroll down, I can see the actual structure of our menu. So we've got a very simple menu here just an unordered list. Notice that we're not using any IDs or classes to identify that, we don't have this wrapped with a div tag or with a nav element. It's just a simple unordered list. Within the confines of an actual site, you're probably going to want to do something to differentiate that list as a menu from other list items, and that could be class attribute, it could be an ID attribute, it could wrap it in a div tag or in a nav element, so it's really kind of up to you how that's structured within your site.
I didn't want to add any of the additional structure to this particular list, because I really wanted to focus on the basics of styling a vertical menu. Now inside the unordered list, we have five separate list items, and of course, inside of those we have our individual links that are making up our menu. All right, so I'm going to scroll up to our styles here in the head of the document, and I've got a little bit comment there for vertical menu style, so we're going to start styling our menu right below that. Now as I mentioned before, the very first thing we want to do is strip out any of the default browser's list styling, and just so you can kind of see what I'm talking about, I'm going to go ahead and preview this in the browser as well.
Now as you can see, since we're just starting out, the browser is representing it exactly as it is an ordered list of links, so we're getting the bullet points beside them. We're getting the default link styling of underscore blue text because we don't want anything guiding the link styling in our file either. So we're really just seeing sort of the browser's default styling. So to go back in my code, I'm just going to go ahead and start stripping out to the default list styling. To do that, I'm going to use a group selector, I'm going to do ul, li and then go ahead and open up and close the curly braces. Now before I get into the actual styling of this, I want to talk about the methods that you can use to strip out the sort of default list styling.
And a lot of times this will be done within a CSS reset, so if you're using a template or some type of a framework, or you're coping and pasting or even building your own CSS reset from another site, check it and make sure that it has a reset for unordered, ordered a lists and list items, most of them do. What that will do is it will strip out all of the default list styling to begin with, and then you have to build it back in for all the menus. If that's the case, and you have that happening earlier in your styles, you can skip this step. You don't have to go ahead and do this twice.
All right, so the very first thing I'm going to do is I'm going to tell it that I don't want any margin or padding to show up. So I'm just going to type in margin, and then I'm going to set zero for that value, and then I'm going to do the same thing for padding, padding of zero as well. Some browsers use margins to control the indent of the list items and the lists themselves, some use padding. They sort of mix it up a little bit, so it's safe to just go ahead and strip out any default margins and any default padding on your list items and your unordered lists. Now that's also going to effect the vertical spacing, the list items, you need to be aware of that, but then again, that's something you really want to be able to control when you build your vertical menu so that's another desired result of doing this.
And then directly underneath that we need to get rid of those bullet points, and the easiest way to do that is just to tell the browser not to display this as a list. So I'm just going to type in list-style, which is the property that I want, and I'm just going to turn it off by typing in none. So I'll go ahead and save that. Now in certain examples online, you might see a shorthand notation here of none outside and none, essentially they're all doing exactly the same thing. By just saying a blanket none, you're telling the browser look I don't want you to display this as a list at all, so no bullet points, no, nothing like that, take any of your default list styling and get rid of it for me.
So I'm going to save that, go back into the browser, and refresh my page, and as you can see, we go ahead and lose the default indentation, and we also lose those bullet points, so it's no longer displaying as a list. With the default list styling turned off, we can now turn our attention to styling the links themselves. In our next step, we're going to define the size of our menu by controlling the actual size of the links themselves.
There are currently no FAQs about CSS: Styling Navigation.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.