Start learning with our library of video tutorials taught by experts. Get started
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.
Now that we've displayed our links horizontally and tamed or floats, we can turn our attention to more familiar tasks, such as defining the sizing and the spacing of our links. To work on that, I have once again opened up the horizontal.htm, this time from the 04_04 file. I've modified the styles. Once again, I removed the nav selector. We're going to bring it back a little bit later on, but right now I just want to focus on controlling the sizing and the spacing of the menu items themselves. Now to do that, we're going to add another selector.
So just blow the grouped unordered and list items selectors, I'm going to create a selector that targets all of the links inside of the list items, so that's li a, and inside that we're just going to set a couple of properties that are going to allow us a little bit greater control over the overall sizing and spacing of our links. The first thing we want to do--remember that link elements display as inline elements by default. I'm just going to go ahead and change that display property to block. Now, since we've floated the list items, changing the display property to block for our links is not going to cause any issues, it'll still display as a horizontal list.
The next thing I want to do is I want to vertically center the text within the link area. One of the ways that we know how to do that is to use line-height, so I'm just going to go ahead and set a line height property here of 2 ems. Remember, if the line-height value is larger than the text, then the remaining space gets split, half of it above the text, half of it below the text to create the line box itself, and that's going to center the text vertically. And then finally, I'm going to add a little bit of padding, and what that's going to do is it's going to give me some interior space within the link region, and there I'm going to do 0 for top and bottom, and then I'm going to do 1 em for left and right.
And of course, what that's going to do for us is going to give us a little bit of space to the left and a little bit of space to the right, and its' going to give us an equal amount of space. Even though the length of the menu items won't be the same just because the words are shorter and longer, they'll have exactly the same amount of space on either side of them. That's a really important styling consideration I want to show you. I am going to go ahead and save this, go back out to a browser and just to kind of show you why I am going to tab through these links. You can see by the amount of space that we're giving these guys, there's enough space for somebody to come through and click.
That's a really important consideration with horizontal links, with vertical links obviously. You are worried about somebody scrolling up and down. With horizontal links the user is going to go from side to side. You want to give them enough space on either side of the text so that it's very easy for them to select it. You don't want to space them so far apart that they are having a travel over the length of the entire page to select one or two links. Now, I want to point out that we are not using any margins here to separate our links, we certainly could if we wanted to. In this case, we're setting it up so that each of the links is resting right against the other links.
Now if that's not what we wanted, we would use a right margin to push those links apart. That defines our link area. And with that defined, we can now turn our attention to their styling and presentation, which we'll do next.
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.