Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the most frustrating parts of creating menus is trying to have the text aligned vertically within the link area. And fortunately, it's not that difficult to do, you just have to understand exactly which parameters you need to control. So to demonstrate this I have the vertical.htm from the 03_04 folder opened in one of my browsers. Let's say I click on the page and just sort of tab through the links. You can see as I tab through the links in our menu right now that the positioning of the text is not vertically centered, it's sort of up a little bit, and I would love a little bit of additional spacing around that text as well so that it's not butting right up against the left edge of the menu, so to do that I need to modify a couple of properties.
So I'm going to go back into our code editor, and here I have the same file the vertical.htm file opened up. I'm going to start modifying the li a descendent selector, the one that's targeting the links within the menu. And the first thing I'm going to do is take a look at the height property. So right now, how tall these particular linked items are, it's been driven by the height property. And I mentioned earlier that for the most part, you probably wouldn't use the height property a lot. To define the height of menu items, there are other things that you could do that would sort of drive that height. Part of that could be padding, for example, the space above and below the content.
But another property that you can use is line-height, and one of the really nice, sort of added benefits of line-height is that when you use it to define a height of an element, specifically when you're dealing with a single line of text, it will center that text vertically within that space. Let me show you what I mean. If I go to the height property, and I change that from height to line-height, and let's go ahead and save that. If I go back out to my browser and refresh this and now start tabbing through the links, you can see that each of the lines is now vertically centered within that space.
Well, the reason that that works is line-height is really more of a typographic property, it's really designed to control the spacing of lines of text within things like paragraphs. But the way that line-height works is an invisible line box is drawn around each line of text. And if the value of the line-height is actually larger than the size of the text, then it's split and half of the space goes above the text and half of it goes below the text, and that's what controls the line spacing. Well, we're using that to our advantage here by using it to vertically align the text within the space we want, which was 2 ems for our text, so it replaces the height value, and it is still sort of gives us that more concern, so it's a really nice property to utilize within the creation of menus.
Now, I do want to add a little bit of additional spacing inside my menu items, so to do that in the same selector, I'm just going to go ahead and add a little bit of padding. And what I'm going to do is I'm going to do .2 ems of padding on the top, I'm going to do 0 on the right-hand side, .2 ems on the bottom and then 1 em on the left, so I'm using shorthand notation here for padding. And when you have all four values like that it kind of goes clock-wise, it starts at the top then it goes to the right, to the bottom and to the left. So top and bottom are getting very minimal amounts of padding, .2 ems on each one of those and then on the left-hand side I'm giving it a full 1 em so it just sort of pushes it away from the edge of the menu a little bit.
So if I save this and go back into my browser and refresh that, you can see now our menu's gained in size a little bit and the text has a little bit more room to breathe. As I tab through each of these items, you can see we have a lot more spacing, the text is centered vertically. It's really what I was looking for. Now, there is one thing you need keep in mind here, adding all that padding is going to add to the overall height and width of the link elements, and in certain layouts that can cause a layout to break. So if the total menu width really needed to be 8 ems, which is I what I set the width of the menu to be, I would have to go back to that value now and subtract the padding that I just added to it to keep the overall width the same.
That's something that trips up people often. Another thing I want to point out here is the line-height technique that we're using here is not going to work across all links. If you have multiple lines of text, it does not center them vertically. For that, you're going to need to research using the Display Table property. It's a little outside the scope of this course, but it is a great way of vertically aligning the content of some of the more complex menus, so I definitely recommend reading up on it.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101819 Viewers
61 Video lessons · 88574 Viewers
71 Video lessons · 72391 Viewers
56 Video lessons · 104093 Viewers
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.
Your file was successfully uploaded.