Start learning with our library of video tutorials taught by experts. Get started

CSS: Styling Navigation
Watching:

Aligning links vertically


From:

CSS: Styling Navigation

with James Williamson

Video: Aligning links vertically

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.
Expand all | Collapse all
  1. 3m 8s
    1. Welcome
      42s
    2. What you should know before watching this course
      1m 12s
    3. Using the exercise files
      1m 14s
  2. 35m 25s
    1. Organizing menus with lists
      4m 26s
    2. Ensuring accessibility
      9m 3s
    3. Using the nav element
      7m 30s
    4. Creating block-level links
      3m 8s
    5. Lab: Structuring navigation
      4m 11s
    6. Solution: Structuring navigation
      7m 7s
  3. 48m 42s
    1. Exploring link style considerations
      9m 2s
    2. Using global link styles
      9m 56s
    3. Styling link states
      10m 57s
    4. Indicating external links
      10m 4s
    5. Styling image links
      8m 43s
  4. 52m 5s
    1. Stripping default list styling
      4m 34s
    2. Defining link dimensions
      6m 0s
    3. Setting link styling
      3m 36s
    4. Aligning links vertically
      4m 11s
    5. Controlling link spacing
      2m 30s
    6. Styling menus with borders
      2m 32s
    7. Creating rollovers
      4m 45s
    8. Restricting link styling
      3m 31s
    9. Lab: Creating a vertical menu
      11m 44s
    10. Solution: Creating a vertical menu
      8m 42s
  5. 54m 58s
    1. Stripping list styling
      3m 35s
    2. Displaying links horizontally
      6m 14s
    3. Clearing floats
      6m 12s
    4. Controlling link sizing and spacing
      3m 11s
    5. Styling links
      7m 16s
    6. Creating rollovers
      5m 52s
    7. Indicating current pages
      4m 43s
    8. Controlling cursor states
      2m 46s
    9. Lab: Creating horizontal menus
      6m 45s
    10. Solution: Creating horizontal menus
      8m 24s
  6. 55m 35s
    1. Overview of dropdown menus
      1m 17s
    2. Structuring submenus
      5m 56s
    3. Styling submenus
      6m 4s
    4. Creating submenu rollovers
      3m 28s
    5. Positioning submenus
      5m 43s
    6. Controlling submenu display
      5m 5s
    7. Creating persistent hover states
      5m 53s
    8. Animating menus with CSS transitions
      6m 29s
    9. Lab: Dropdown menus
      6m 51s
    10. Solution: Dropdown menus
      8m 49s
  7. 58m 7s
    1. Creating CSS-only buttons
      8m 39s
    2. Creating special effects for buttons
      4m 2s
    3. Enhancing buttons with gradients
      7m 40s
    4. Overview of CSS sprites
      3m 30s
    5. Using CSS sprites for icons
      14m 30s
    6. Styling block-level links
      8m 38s
    7. Lab: Enhancing navigation with CSS
      5m 26s
    8. Solution: Enhancing navigation with CSS
      5m 42s
  8. 6m 29s
    1. Additional resources
      6m 29s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
CSS: Styling Navigation
5h 14m Beginner Nov 16, 2012

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.

Topics include:
  • Organizing menus with lists
  • Creating block-level links
  • Styling links, link states, and image links
  • Defining link dimensions
  • Controlling link spacing in a menu
  • Creating rollovers
  • Clearing floats
  • Indicating current pages
  • Controlling cursor states
  • Building dropdown menus
  • Creating CSS-only buttons
Subjects:
Developer Web Web Design Web Development
Software:
CSS
Author:
James Williamson

Aligning links vertically

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.

There are currently no FAQs about CSS: Styling Navigation.

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Styling Navigation.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked