Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
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.
Just as with our vertical menu, the choices that you make for the visual style of the menu are what really sets it apart and makes it unique. You're free to use colors, gradients, images, icons, font formatting, and all sorts of custom styling to get your menus to look exactly the way that you want them to. We're going to get our menu closer to the desired styling by controlling the visual formatting for multiple elements. So in this exercise, I want you to pay close attention to how the styling of several elements is going to combine to create the final menu.
Here I have the horizontal.htm open from the 04_05 directory. I'm going to scroll down until I get to my list styling here. So we're just picking up right where we left off before. Now the first thing I want to do, I'm going to sort of revisit that nav selector that we created earlier. If you remember from looking at the finished version of the horizontal menu at the beginning of this chapter, it has a two-toned bar, a sort of a lighter orange color, and a darker brown color underneath that.
So to start that, use the nav element. Remember, that's the very top parent element for our menu. So I'm just going to go to nav, and inside this selector, I'm going to set a background color, and I want the background to be #916A31, and that's a sort of darker orange color, if you will. I'm going to go ahead and save that. And if I preview this in a browser, the nav element is stretching the entire length of its parent element, which in this case is the body tag.
And I want to point out something about the height of this. You notice that we did not specify a height on the nav, so the nav's height currently is being controlled by its contents, which in this case is the line-height on each of the link items. The default styling here of the blue text and the underline, that's probably not going to cut it for this menu. So let's go back into our styles here for the list item a selector, the one targeting links inside the list items. I'm going to go inside that and change the color to white for the text.
For text decoration, I'm just going to go ahead and remove that underline by typing none. So I'm going to save that, go back into the browser and refresh that, and that looks a lot better. So now we have white text, no underline, and remember, anytime you're going to remove the underline-- which is very common in menus--most people will remove the underline in menus. You have to give some other visual indication that it's a link. Being part of a menu structure is a very strong visual indication in this part of the link, and of course we are also going to have some rollovers that's going to help with that as well.
You may have remembered from earlier that we have that sort of two-toned look to this horizontal menu bar. So we had the lighter orange that was the much wider part of the bar, and then that darker orange color you're seeing currently into the nav is being a very smaller portion of it. So how is that effect achieved? Well, we could have used a border, we could have given the nav element the brighter orange color and then used a border on the bottom of it. Of course, there is always more than one way to achieve an effect, and sometimes you have to choose a method based on what else you have going on within that particular element, in this case the menu.
So let me kind of show you how we're going to be building this. I'm going to take the ui, li group selector, and I'm going to create another selector just below that. I like keeping things kind of in the order that they appear. So I'm going to type in a ul, I'm just targeting the unordered list now. I'm isolating it as apart from the styling here. And I'm going to give it a background color, the same way I did the nav element. Now this is going to be that brighter orange color so that's #D5973C, and we've used that color so much in this course.
And I'm sure some of you guys already have that memorized. We're going to do something a little different here. I'm going to go ahead and define a height for this unordered list. So I'm going to use 2 ems. You may have noticed that that's exactly the same as the line-height. So you are probably saying to yourself, well, isn't that kind of redundant? For right now it is, but keep that in the back of your mind that there is a reason that we're doing that. And then I'm also going to go ahead and set the width of that to 100%. Now why am I doing that? Remember when I told you earlier that floated elements tend to sort of shrink wrap to their content? A normal block-level element would expand to fit its parent container.
But for floated elements, unless you define a width for that element, it tends to vary based on where it is found. So I'm going to go ahead and define a width of 100% on that. I'm going to save that, go back into my browser, refresh the page. The bar there changes color from that sort of darker brown orange to the brighter orange. Now what's happening here? Well, these elements are nested, so the nav element is the parent element, the unordered list is inside of that, list items are inside of that and then finally the links are inside of that.
The way that that's painted on the screen is the parent element is painted first. So if you will, the nav element's behind the unordered list, and since they're both the same size right now, 2 ems, they're laying right one on top of each other, but we're going to change that. Let's go back into our code, and I want to go back up to the nav element, and I'm going to add another property to that, and I'm going to add height to it, and I'm going to give it a height of 2.3 ems. So, as you can see, that is 0.3 ems taller than the unordered list.
Essentially, what we're doing is we are defining heights for both of these guys so that a little bit of the nav element will be showing through or underneath the unordered list, because it will be just a little bit taller. So in essence, this time we're using a line height, not so much to define the overall height of this particular menu bar, but to center the text within a certain area or region. So I'm going to save this, go back into my browser, and refresh that, and there you go, that's exactly what we're looking for. We've got that two-toned bar now, we have got a little bit of that brown sticking out underneath it, and we have the orange on top of it.
I know there's some of you out there right now that are saying, well, why didn't you just go ahead and use a border for the bottom of that in that brown bar, wouldn't have that been a little easier? It probably would have been easier, but one of the things I wanted to show you guys was that you can use all of these different elements as styling hooks. You have this nav element or div, if you're going to put a parent element on your menus, you have that element. It's available for you to style. You also have the unordered list, you also have the individual list items and the links, and in sort of layering these, you can create some really complex structures to your menus.
You could combine gradients, you could combine images and icons with backgrounds. You can have certain backgrounds that are a little bit see-through when others that aren't so. You can do some really powerful things, and I wanted to demonstrate that. Plus, we're going to be doing some rollovers in a moment. They are going to take advantage of the fact that the unordered list is a little shorter than the actual nav element itself. Well, that has got us almost finished, believe it or not. The majority of our menu styling is complete. All that is left for us to do is to create the rollovers, then after that we're going to tackle an optional technique of indicating the current page for menu.
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.