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

Positioning submenus

From: CSS: Styling Navigation

Video: Positioning submenus

Now that we've gotten the styling of our submenus taking care of, we can turn our focus to positioning the submenus exactly where we want them. There are multiple approaches that you can take when you're deciding how you want to position submenus relative to the top menu and many of them depend on really kind of how you plan on controlling the display of the submenus. The approach that I'm going to show is based on using absolute positioning for the submenus. The reason that I favor this technique is that it keeps the submenu text accessible to screen readers, which to me is a big deal.

Positioning submenus

Now that we've gotten the styling of our submenus taking care of, we can turn our focus to positioning the submenus exactly where we want them. There are multiple approaches that you can take when you're deciding how you want to position submenus relative to the top menu and many of them depend on really kind of how you plan on controlling the display of the submenus. The approach that I'm going to show is based on using absolute positioning for the submenus. The reason that I favor this technique is that it keeps the submenu text accessible to screen readers, which to me is a big deal.

Now we are going to discuss that aspect in a little bit more detail later on in the next exercise, but for right now I want to focus on setting the initial position of our menu, and the reason that I have it previewing here in the browser right now is I wanted to show you what the default position of the submenu is. So if you just leave it alone and don't position it at all, this is where it shows up, and that's actually not bad. That's precisely more or less where we want it. Remember, this is a nested list to this list item, and the reason that it's positioning the way it is, is because remember, we have stripped off all of the default list styling, we have stripped off all default margins and padding on the list so it's not indenting, it's going to show up directly below that.

So a lot of times you can take advantage of that default location and just not worry about positioning it at all. Now the way that we are going to be doing this is we are going to take the submenu, and we are going to position it way off the screen initially so that it'll be hidden. And then when somebody rolls over one of these top menu items, we'll bring it back using absolute positioning and put it right where it needs to go. That's one way of doing it. Other methods take the display property and change it to none or the visibility property and change it to hidden, and those work just fine, but my problem with those techniques is the screen readers, if they see an element whose display is set to none, they'll ignore it because they're thinking that the designer wants this content hidden at the moment, so I am going to skip over it.

It's very important since this is navigation for to be accessible, which is why I like the method that we are going to be using. I am going to switch back over to our code. I am working with dropdown.htm, which is found in the 05_05 directory. I want to point out something really quickly. Let's scroll down. First thing we are going to do is we are actually going to style up in the top menu, directly underneath this unordered list, which is on about line 56, I am going to create a list item selector. The only thing I'm going to do there is set positioning to relative.

Now the reason that I am doing that is, remember, for the submenus we are going to be using absolute positioning. If you've never worked with the absolute positioning before, what it's going to do is it's going to go up into its parent elements, and it's going to ask its parent elements which one of you guys has a positioning property. And it's going to keep going up until it finds one, and when it finds one it says, okay, I going to position myself relative to you. So by putting position of relative on the actual list items themselves, what we're doing is we are saying this is going to be our point of reference, this is the parent element that has positioning applied to, so I am going to position myself relative to that list item.

The next thing I want to point out to you is the links themselves, and this is our top-level links. Keep in mind that their line height is 2 ems, and that's actually creating the size, if you will, it's creating the height of the list as well, so the unordered list is 2 ems and the line-height of the link items themselves are 2 ems ,so our top-level menu is exactly 2 ems tall. We should keep that in mind as we write this next selector. I am going to scroll down, and I am going to find our ul.submenu selector, that's who we need to position, we need to position the submenu itself which is an unordered list, and I'm going to go down and just add two more properties to this.

I am going to add position and I am going to change that to absolute and then on line below that I am going to set the top value to 2 ems. Changing something to absolute positioning removes it from normal document flow and kind of floats it above the page. These values are offset values that you can set, top, left, bottom, and right, we'll line up with the edge of the element, and you will position it wherever you ask it to. If we were to set top to zero, it would align itself to the very top of the list item, which would be the top of the unordered list above it.

So by saying 2 ems, we are actually pushing it down, we are pushing it down from where it would be positioned, we are not saying anything about left or right. So what it'll do by default is it will position its leftmost edge against its parent container as leftmost edge. In this case, that's a list item, so it should line up perfectly. So if I save this, go back into my browser and refresh the page, it doesn't look like much has happened, but actually a lot has happened. You'll notice, again, that it's positioned in exactly the same place, but if you remember earlier, every time we would hover over products, it would actually push this menu down, and the reason it would do that is because the size or the height of this products menu item would actually grow.

Since the submenu below was in normal document flow, it would push it down as well. By using absolute positioning, it doesn't really care that products is getting bigger, it's not going to push it down at all, and since we have this little buffer between the nav and the ul, you don't ever see that push happening, so it just stays right where we want it to stay, and it's positioned exactly where we want it. Now this is actually not the default positioning for the submenu. This is how our submenu is going to appear when somebody is interacting with its parent menu item. The reason that I chose to do this value first is I always like to make sure it's exactly where I want it before I get it off the screen because it's a lot harder to test them, so we are just sort of putting it in place, and so in the next exercise, what we are going to focus on is controlling that display, making sure it's positioned initially where we want it, and then using this hover event to trigger the menu positioning itself in this location.

Show transcript

This video is part of

Image for CSS: Styling Navigation
CSS: Styling Navigation

53 video lessons · 17020 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.