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.
One of the most important properties of any menu is the dimensions of the actual links themselves. This creates the clickable area that users interact with when they select them. You want to make sure that you create a clear , active area without using too much or too little space. So in this exercise, let's examine a few of the ways that we can define link dimensions. Now, before we get into actually defining them, I want to show you what the current link dimensions are. So I am going to go ahead and open up vertical.htm from the 03_02 folder--that's really just sort of picking up where we left off in the last exercise--and I want to show you what the size of the current active area of those links.
So I am just going to click somewhere on the page, and hit Tab to be able to tab through those links. Now you can see that right now the only area that's active is the content itself, and this is basically due to the inline nature of the anchor element. The anchor tag or the anchor element is an inline level element, and what that basically means is it sort of shrink-wraps itself to the content that's inside of it. So if we want that clickable area, that active area to be a little bit larger--which we certainly do within a menu--we are going to need to be able to define those areas with a little bit more precision, and the first thing we are going to do to do that is to change the display property of those links.
So I am going to go back into my code editor, I am going to scroll down and find the existing vertical menu styles that we have, go ahead and create another selector. Now, this time I am going to create the li a selector. So, now I'm only targeting links when they are found within a list item. So again, that's not going to be global for all the links on my site, just ones that are found inside of a list item. Now again, you know, if we were being a little bit more specific with the menu, we may go in and actually use an ID or a class to help us identify this. Right now we are just staying fairly generic with our selectors.
Okay, so the first property I am going to go ahead and set is the display property, and I am going to change display from inline to block. So essentially what that does is it tells every single one of these links that they need to display as a block-level element, so if I save the file, go back into my browser, and refresh the page, now as I tab through, you see there's a big difference in the clickable area of the link. Now the reason for that is that block-level elements by default are going to stretch to fit the width of the container that they're inside of.
In this case, both the list item which is block-level stretching out, the unordered list is also block-level, and it's stretching out and so that allows the link which is now block-level inside that to stretch out as well, and they are stretching out all the way to the edge of the body element. So they are really just stretching out until they find a parent with a fixed set of dimensions. So now the clickable area of these links actually extends, and you can even see if I hover over on this side, it extends all the way out to the edge of the page. That's useful because you don't want to make people hover over just the text, you want to give them a little bit of grayish area, obviously, if they are mousing over a menu.
But you probably didn't want to stretch for the entire length of the page in this case either. So the next thing we need to do is we need to go in and set a width for these elements. Now, you can do this on one of the parent elements, so it would be fine to go in and take the unordered list tag for example and set a width on that. But in this case, we are just going to keep the styling directly on the link. So after display block, I am going to hit Return to get onto the next line, and I am going to type in width, and I am going to make that width 8 ems. Now I will save that, go back to my browser, refresh the page, and now as I tab through to begin, you can see that they are much more defined in terms of the area that they are inside of.
So, now if I mouse in this area out to the right of it, I am not activating them, only when I come into the area that I wanted for this particular menu are, they going to activate. And notice, they are activating all the way across, even if the text isn't stretched across, which is perfect, that's exactly what I was wanting for that. So I am going to go back to my code, and I want to do one more thing as well. I noticed, for example, that we were able to stretch the width out, but we didn't do anything about the height, the height is still being defined by the contents of these links. In that case, again, we are down to using the actual text of the links to define the height.
So just the same way you can define width, you can define height, so if I go down to the next line, and I type in height, I can set that to say 2 ems, and I'll save that, come back in, we'll refresh that page in the browser. Now it looks like I'm giving them a little bit of extra spacing, but I'm really not. If I tab through them again, you can see that they are still butting right up against each other, they are just a little bit taller, and at this point at 2 ems' worth of height. You may be wondering, why I am using ems instead of pixels or some other unit of measurement? It really depends on what you're working on and how you're working.
If it's a flexible layout, meaning that you have designed it to flex based upon the size of the browser or the size of the screen that you are viewing it on, then ems are a perfect solution for that. So if somebody says we are to increase the size of the text, you'll notice that the relationship between the menu and the text itself remains and they're not shrinking down or getting overly large, they are just staying the same relative size, which is pretty nice. That's going to be very helpful if this is viewed on say a mobile device because the menu will just shrink down to fit basically on the size of the text. So that's one of the reasons why I am using ems here.
You are not required to use ems, it's really just a personal preference. Now, one more thing you'll notice, again, as I tab through these that the text isn't centered in any way shape or form. I mean, it's sort of butting up against the top of the element. And we use the height property to set that, but I think you will very rarely find yourself actually using the height property. I wanted to show you that that you could certainly do that once you have changed them to block- level links, and the fact of the matter is there are other ways to control the height of those elements that do more things for you, for example, vertically aligning the text, so a little bit later on in the chapter we'll see some of those techniques as well.
So, yeah, I know the links there are not much to look at quite yet, but we do have some clearly-defined link regions, and that's something that you want to sort of get out of the way before you begin worrying about the actual presentation of the links themselves.
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.