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

Creating persistent hover states

From: CSS: Styling Navigation

Video: Creating persistent hover states

Although our dropdown menu is functioning properly, it is not quite perfect yet. As we saw in the last exercise, when we roll over the submenu items, our top-level rollovers go away. We need to create a persistent hover state that's going to keep that rollover active as we browse through the submenus. We really don't need to do a lot to accomplish this. All we have to really do is just update a couple of our existing selectors. So, this time we are going to be working with the dropdown file again, but this time from the 05_07.

Creating persistent hover states

Although our dropdown menu is functioning properly, it is not quite perfect yet. As we saw in the last exercise, when we roll over the submenu items, our top-level rollovers go away. We need to create a persistent hover state that's going to keep that rollover active as we browse through the submenus. We really don't need to do a lot to accomplish this. All we have to really do is just update a couple of our existing selectors. So, this time we are going to be working with the dropdown file again, but this time from the 05_07.

And we're actually going to be styling up in the top-level menus, so make sure you're looking up at the horizontal menu styles. I'm looking for the selector that controls the rollovers for our top-level menu items, and it would be this little guy right here, so li a:hover. Now, what's interesting about this one is this is really targeting any link that's being hovered over, which is also inside of the list item. And that includes all of our submenus, and that's one of the reasons why we had to create this selector. As specific as it is, it's overriding the top-level hover styles.

What we really need to do is find a way to target only those top-level links. And to do that, we're going to utilize one of the newer selectors that CSS has given us. Just after the li a:hover selector, I am going to type in a comma to group another selector with it, and I am going to go ahead and type in topmenu li:hover a. Let's talk about who the selector is going to target. It's going to look inside the topmenu, which is the top-level unordered list, it's going to find any list item inside of that that's being hovered over, and then it's going to find any link that's inside a list item being hovered over.

So, it's really saying any link, any list item inside topmenu--and it's not being very specific--it's saying any one of them, no matter how far down the tree you go. That means that this selector would still apply to all the links also in our submenus. So we need to change that. I am going to place my cursor between the topmenu and the li. Then I'm going to do a space. I am going to type in a greater-than symbol or a right-angle bracket, depending upon how you like to refer to it. Now, make sure that there is space on either side of that. This is what's known as a child combinator.

If you've never used it before, what a child combinator does for you is it's very similar to descendant selector, but it has a way of ending it right inside that element. So it's basically saying, okay, look inside that top-level menu and find any list item that is a direct child of that menu. So, it doesn't say just how far down give me all of the list items. It says, no, no, no, just inside the top menu. That would return all the list items inside the top- level menu but none of the list items in the submenus.

Now, after that we are still using a descendant selector. Now we're saying give me any links. So, that's still targeting the links that are inside the submenus. So, what I am going to do now is I am going to add another child combinator to it. Now, I know this looks really complex, but trust me, your browser can handle this no problem at all. So what this is doing is it's saying, hey, look inside the top menu, find the list items that are a direct child of my menu, and then find the links that are the direct child of that list item. So if we go into the code, the actual structure, what this is going to do is it's going to say, hey, find this, find the list item that is a direct child.

So this would match, but this would not because this is not a direct child. Then it would say find any links that are direct child. So this would match, but this would not. So it's a nice way of targeting just those top-level links. Now, what's really nice about this, again, it's going to persist as long as I'm hovering over this list item. Because the list item also includes the submenu, that means that this will stay active as long as you are mousing over that list item, which of course, as I mentioned includes the submenu. So if I save this, go back into my browser, and refresh the menu, sure enough, when I mouse over this now, as I mouse down, you'll notice that this persists, which is perfect.

That's exactly what we want. On the surface, it looks like everything is fine until I mouse over the current one. That's one of the real problems with writing a selector that's that specific. That's pretty easy to take care of. All we really need to do is I am just going to go right back down to the selector below it that's controlling that current styling, and I am just going to group another selector with that, I am going to say topmenu li:hover a.current. I don't really need to use child combinators here because I can be very, very specific in saying any link that has a class current applied to it, when its list items are hovered over just go ahead and persist the styling.

So, saving that, refreshing the browser, so now not only does our submenu work, but now it leaves our current styling alone as well. So there you have it. Our dropdown menu is now fully functional. So all we need to do really is to go ahead and turn on the rest of the submenus. And if we ever wanted to create another one, we could just go ahead and create another submenu and the styling would apply to it. So I am going to go back in my code right now, and I want to get rid of the comments that we have, and yes, I am taking advantage of Dreamweaver here.

I am so sorry, but you could just delete those comment tags, save that. Now, I want to come back to the page. Notice that not only is products working, blog is working, and contact is working, all three of our submenus are performing exactly the way we want it to. So really, if About needed another submenu, all I would have to do is go in and structure it with the HTML, and the styles would go ahead and take care of all the functionality and appearance of the submenu. Now, if we wanted to create multilevel dropdowns, that would require a little bit more work, but honestly, the techniques behind them are exactly the same as we've used here.

So, once you've experimented with building single-level dropdowns, see if you can figure out how a multi-level menu might work.

Show transcript

This video is part of

Image for CSS: Styling Navigation
CSS: Styling Navigation

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