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.
Before we can start styling our submenus, we first have to create them. Submenus are lists of links that are nested inside their parent menus. In order to properly style your submenus, it is critical that they are structured correctly. And let's take a look at how to do that. So I have the dropdown.htm file open from the 05_02 directory--I'll scroll down in the code a little bit so I could focus on our existing menu. We are going to have three submenus as a part of this: products, blog, and contact are all going to have submenu options.
Structuring this is actually really simple, it's just a nested list but you've got to make sure that you are placing them in the right place. In the case of products, for example, what I'm going to do is I am just going to create a little bit of space inside the list item, and this is what's really important about this. You want your submenus to come after your link but still be inside the list item, you don't want to stay outside of it, and it's just another unordered list in this case. So all I am going to do is inside that list item, I am going to create an unordered list, and then I am going to go ahead and close that unordered list.
That's just a habit that I've gotten into over the years. Whenever I open a tag up that's going to have a lot of stuff inside of it, or multiple items inside of it, I'll almost always close it right off the bat. That way I don't forget to close it later on. So it's just a habit that I've gotten into. Inside this we are going to have our first list item, and it's just going to be structured exactly like the menu. Inside this list item, there is going to be a link, and this one is going to go href to laptops.htm. I am just going to give it a title of laptops and then the text is just going to read Laptops, then I'll close out both my list item and my link.
Now I'm a big fan of not typing more than I have to, so what I'm going to do is I am just going to copy this and then paste it down on the next line and just change the appropriate text. So in a place of laptops, we are going to have tablets.htm, the title is going to be tablets, and of course the text is also going to be Tablets, and I am just going to keep doing that copying and pasting. The next one is going to link to phones.htm, the title for that one is going to be smartphones, the text is going to be Smartphones.
Finally, we are going to have accessories.htm, that of course we're just going to give it the title of accessories, and then finally it will also say Accessories. So I am going to save this, and just to kind of see what this is looking like, I am going to preview this in one of my browsers. Maybe it's not exactly what we are looking for, but it's also not horrible either. There is the menu directly underneath my Products one. Of course, I want it to look more like a vertical menu, but we'll get to the styling a little bit later on. What's important is you can kind of see the structure of this, you can see that it's directly underneath products, it is a nested list inside of the Product list item.
We have two more of these to do, and rather than making you watch me type all two of these, I am going to use the magic of video editing. So hang on just one second, and just like that, I now have a finished menu with all of my submenus in there. Now just to show you where these are, just pause the video and type these in. We've got a submenu for blog, it's going to have the Recent articles, Archives, and Hall of fame, and then we also have a submenu for Contact, I'm going to scroll down a little bit to show you that one in its entirety.
And that one has Customer service, Register, and Technical support. So remember, they need to be inside the list item. The last thing we are going to do is I am going to identify all of the submenus with a class, and I am also going to identify the top menu with a class. Now that's going to help us out when we style our dropdown menus, but that's not the only reason we are doing it. I am actually doing it because it's nice and semantic, and it lets any other user-agent or person know when they are looking at the code what type of menu this is. So I am going to go up to the very top unordered list, and I am going to give that a class of topmenu.
And then for each of the submenus, I am going to come in and give them a class as well, and I'm just going to call it submenu. Through the use of a lot of the newer CSS selectors, we really don't have to use classes anymore when we're styling our dropdown menus. It used to be almost a requirement that anytime you wanted to do a dropdown menu you had to have a class attribute that would allow you to target those submenu elements. So again, we've got three submenus. You want to identify each of them with a class of submenu, and you want to identify the top unordered list with a class of topmenu.
But these days, we actually have a lot of really cool selector types that allow us to target specific items and really filter out what you're selecting. So we don't necessarily need to go that route anymore, and I've seen a lot of tutorials online where people will do dropdown menus and they'll say, well, you don't need to use classes anymore, and that's true, that's absolutely accurate. However, I continued these classes for a couple of reasons. Number one, it does make it a little bit easier for you to style. You don't have to rely on selectors that might not see support in some of the older browsers, but number two, it's just a little bit more semantic.
Now as you're looking through the code, it's really easy to identify when you're looking at a submenu versus when you're looking at a top-level menu. So that's the reason that I use classes. The structure of our menu and all of our submenus is now finished. We are ready to go ahead and start styling it now, but before we do that, I just want to reiterate that the most important thing for you to remember here is where you locate the submenus. Remember that you want to locate them within the list item that it represents and not inside the anchor element. If you fail to nest that list properly, it's going to result in not only invalid code, but it's also going to result in dropdown menus that are going to be impossible for you to style.
So make sure that you are paying extra attention to your page structure as you code your submenus.
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.