Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Aligning links vertically

From: CSS: Styling Navigation

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.

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.

Show transcript

This video is part of

Image for CSS: Styling Navigation
CSS: Styling Navigation

53 video lessons · 16721 viewers

James Williamson
Author

 
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

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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.


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.

Join now "Already a member? Log in

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
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.