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.
It's not uncommon to want a menu to indicate the current page which tells the user which page or section of the site that they're currently viewing. Well, there are a number of techniques that allow us to do this. We're going to focus on the most common, which is to style a class attribute. So working in the same file, we've been working in the horizontal, this time from the 04_07 directory. The first thing I want to do, I'm going to scroll down to the menu, and in the menu I'm going to indicate the current page by using a class.
So in this case, I'm just going to do the homepage, and I'm going to apply a class attribute, and I want to create a class called current. There are a lot of different ways that you can do this. If you're hand-creating a site, you can certainly go in and just on every page go ahead and apply that particular class to the current menu item. You can certainly automate that process. There's lot of blogging software out there that will apply that class dynamically for you. So there are a lot of different ways to do that, but for the most part ,people are going to be applying a class that indicates exactly what the current page or section is.
Now that we have that to identify through a class, we can go ahead and create a class selector. So I'm just going to do a class selector for current that allows us to target that link only. First thing I'm going to do is just give it a different background color and a different foreground color to really set it apart from the other links. So I'm going to do a background: #AD9B7F. It's sort of a desaturated brown color, and then I'm going to do a foreground color of #eee which is very light gray, almost white, but not quite.
So essentially what I'm doing is I'm taking both of those colors that darker brown color and the whiter the text, and I'm desaturating both of them, coming in the same direction so they're actually going to have less contrast. So I'm going to save this, go into my browser, and refresh it, and you can see the homepage is now being indicated by that lighter tan color, and even the text is sort of dulled a little bit so that it's separated from the rest of the menu. I would like that current indicator to look a lot more like our rollovers, in the fact that I'd like it to have that sort of tab look to it.
What's nice is we kind of already have that styling. We don't need to do it again if we don't want to. I can just go right up here to the hover and grab all of that tab styling. I don't need the height, although I certainly grab it if I wanted to. I could just go ahead and copy that. I'm going to paste it into the current selector, save that and then refresh it, and indeed now we have that tab look on our current page. Now there's one little thing here that's kind of bugging me about this. That brown bar that's underneath it really doesn't fit now.
When we do one of our rollovers, it really sort of fits because it looks like it's just sort of popping up a tab, but for our current indicator it doesn't really look all that great. So let's address that by using one of the methods that we've kind of talked about earlier. We'll put a border on the bottom of that. So I'm just going to do a border-bottom. We'll do 0.3 ems, which we know as the amount of space that we have there for that nav bar. We'll make it solid, and again, I'm going to use the same sort of desaturated trick. I'm going to do 917F63.
It's a little bit darker than the rest of the indicator so it will still give that appearance of a bar underneath it, but it'll be desaturate as well. So I'm going to save that, go back, refresh, and you can see we still have that two-tone thing going on there, but it definitely stands out. If you start rolling over these different elements, when you roll over the homepage indicator, the rollover still kind of works. That can be very distracting to somebody if it's the current page, and you roll over it and it's active, you're like, well, wait a second, maybe that is a separate link. And they click on it and they arrive right back where they were.
So let's go and take care of that rollover by going right up to the same selector typing in comma a:hover.current. So what that's going to do for you is it's going to extend the current styling to not only any element that has the current class applied to it, but when that element happens to be a link, and when that link is being hovered over apply the styling as well. So now if I save this, refresh, when I rollover it, nothing really changes.
Now there is one more minor detail here that I want to take care of. As you can see, as we roll over this, the rollovers don't work anymore, but the cursor still changes to that pointer since it is an active link. Well, thankfully CSS allows me to control that, and we're going to do that in our next movie.
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.