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.
As with vertical menus, you use the hover pseudo-class selector to create rollover effects for horizontal menus. In this exercise I want to take our rollovers a little bit further than we did in our vertical menu by creating a tabbed look for them. I'm working again in the horizontal.htm. This time from 04_06 directory, and of course we are going to have to create a new rule for our rollovers. So I am just going to go below the last rule, which is the li a rule, and I am just going to do li a:hover.
So the same thing we've kind of been doing to create rollovers. We are using the hover pseudo-class selector and targeting the links inside the list items. The very first thing I am going to do is apply a background color to this, and I am probably going to apply a little something you're not expecting. I am going to do red first. Now one reason I'm doing that is because as we go through the process of creating these rollovers, it lets you see exactly what area is defined as the rollover. So if I were to save this, preview this in my browser, as I roll over these links you can see the height of the links isn't quite tall enough to go down into that bottom area.
It's the same height as the unordered list itself, which is the orange background. So the first thing I am going to do is set a height property, and I am going to set the height property to 2 ems. Now it may seem kind of weird that we are setting the height for a rollover when we already have defined line height for the links themselves, but bear with me here. On the next line we are going to do a little bit of padding on the top sides. So padding-top, and we are going to do 0.3 ems. Now, if you add these two together, you get 2.3 ems, which of course is we know is the exact height of the nav.
So if I save this, preview this in the browser, now as a I hover over this, you'll notice that it is indeed taking up the full height of the nav element, but notice that the line height isn't quite centering it. So we are getting a little bit of indentation. It's like going down a little bit. If that's the effect you're going for, awesome, but that is not the effect we are going for. So let's see how we can create that, and remember we wanted them to grow in height. We want them to like tabs that are tabbing above the menu bar.
We don't want them to go down like they are going right now. So I am going to correct that by going down to the next line and doing a position of relative and then a top value of the -0.3 ems. Now relative positioning is going keep this element in normal document flows. So it's not like absolute positioning ,that's just going remove it from belonging to its parent element. But what it does allow us to do is offset that element, and we are offsetting it by a top value of -0.3. So it's just going to move it up.
So a positive top value would actually move it down, a negative value is going to move it up. Some of you maybe wondering, well, why wouldn't you just use a negative top margin? You could, and that would certainly work. My issue with using a negative top margin is these menus very rarely exist in a vacuum. They're always going to have elements above them and below them. And if you start doing things like negative margins and moving things around, you have a real chance of causing a conflict with an element above or below it. So by just leaving it out of the equation and doing relative positioning, you sort of get around that.
So I am going to save this, go back to the browser, and refresh. Now when I do a rollover, check it out. We have exactly what we want. Our rollovers are taller, they are going up instead of down, and because we haven't adjusted the line height, the text is staying exactly in the same place as it was before. That's perfect. Another thing I would like to do is kind of round those top corners. The easiest way to do that, in the past we had to use images to do that, and it was a real pain, because you would have to slice up of all these different background images and use all this markup, and it was a real pain.
But since the advent of the support for many of these CSS3 properties, we have been able to do things like that through pure CSS. And the property that we are going to use here is border-radius, border-radius. This is widely supported among most modern browsers. So widely supported, in fact, that it's been un-prefixed, meaning we don't have to put -webkit or -moz in front of it. We can just say border-radius. I wanted rounded the corners for just the top corners. So I am going to do 0.3 ems space, 0.3 ems, space 0, space 0.
So that was top left, top right, and then the bottom two corners get 0. So I'll save that, preview that again, and now when I do a rollover, there is our tabs in all of their glory. About the only thing that's not going right for us right now is the red color is kind of distracting. I am going to go back into my hover, and I am going to change the background color from that red color, and I am going to make it the #916A31. Now that is color is that brown up top, so feel free to copy and paste it if you like.
I am going to go ahead and save that, go back to my menu, refresh, and there are our rollovers.. A really nice thing about this is the fact that those two colors are the same. It really makes it look like a nice seamless tab, and it looks like it's being tabbed up from the bottom of the bar which is the exact stylistic illusion that we were going for. I want to you to note how we're using the nav, the ul, and the links all together to form the finished styling. Now structuring your menus properly by using elements like nav and ul is going to give you a number of styling hooks.
So don't be afraid to use them when you're styling your menus. We're going to finish up our chapter next as we take a look at a method that allows us to indicate the current page.
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.