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

Creating rollovers

From: CSS: Styling Navigation

Video: Creating rollovers

As with vertical menus, you use the hover pseudo-class selector to create rollover effects for horizontal menus. In this exercise I want to take our rollovers a little bit further than we did in our vertical menu by creating a tabbed look for them. I'm working again in the horizontal.htm. This time from 04_06 directory, and of course we are going to have to create a new rule for our rollovers. So I am just going to go below the last rule, which is the li a rule, and I am just going to do li a:hover.

Creating rollovers

As with vertical menus, you use the hover pseudo-class selector to create rollover effects for horizontal menus. In this exercise I want to take our rollovers a little bit further than we did in our vertical menu by creating a tabbed look for them. I'm working again in the horizontal.htm. This time from 04_06 directory, and of course we are going to have to create a new rule for our rollovers. So I am just going to go below the last rule, which is the li a rule, and I am just going to do li a:hover.

So the same thing we've kind of been doing to create rollovers. We are using the hover pseudo-class selector and targeting the links inside the list items. The very first thing I am going to do is apply a background color to this, and I am probably going to apply a little something you're not expecting. I am going to do red first. Now one reason I'm doing that is because as we go through the process of creating these rollovers, it lets you see exactly what area is defined as the rollover. So if I were to save this, preview this in my browser, as I roll over these links you can see the height of the links isn't quite tall enough to go down into that bottom area.

It's the same height as the unordered list itself, which is the orange background. So the first thing I am going to do is set a height property, and I am going to set the height property to 2 ems. Now it may seem kind of weird that we are setting the height for a rollover when we already have defined line height for the links themselves, but bear with me here. On the next line we are going to do a little bit of padding on the top sides. So padding-top, and we are going to do 0.3 ems. Now, if you add these two together, you get 2.3 ems, which of course is we know is the exact height of the nav.

So if I save this, preview this in the browser, now as a I hover over this, you'll notice that it is indeed taking up the full height of the nav element, but notice that the line height isn't quite centering it. So we are getting a little bit of indentation. It's like going down a little bit. If that's the effect you're going for, awesome, but that is not the effect we are going for. So let's see how we can create that, and remember we wanted them to grow in height. We want them to like tabs that are tabbing above the menu bar.

We don't want them to go down like they are going right now. So I am going to correct that by going down to the next line and doing a position of relative and then a top value of the -0.3 ems. Now relative positioning is going keep this element in normal document flows. So it's not like absolute positioning ,that's just going remove it from belonging to its parent element. But what it does allow us to do is offset that element, and we are offsetting it by a top value of -0.3. So it's just going to move it up.

So a positive top value would actually move it down, a negative value is going to move it up. Some of you maybe wondering, well, why wouldn't you just use a negative top margin? You could, and that would certainly work. My issue with using a negative top margin is these menus very rarely exist in a vacuum. They're always going to have elements above them and below them. And if you start doing things like negative margins and moving things around, you have a real chance of causing a conflict with an element above or below it. So by just leaving it out of the equation and doing relative positioning, you sort of get around that.

So I am going to save this, go back to the browser, and refresh. Now when I do a rollover, check it out. We have exactly what we want. Our rollovers are taller, they are going up instead of down, and because we haven't adjusted the line height, the text is staying exactly in the same place as it was before. That's perfect. Another thing I would like to do is kind of round those top corners. The easiest way to do that, in the past we had to use images to do that, and it was a real pain, because you would have to slice up of all these different background images and use all this markup, and it was a real pain.

But since the advent of the support for many of these CSS3 properties, we have been able to do things like that through pure CSS. And the property that we are going to use here is border-radius, border-radius. This is widely supported among most modern browsers. So widely supported, in fact, that it's been un-prefixed, meaning we don't have to put -webkit or -moz in front of it. We can just say border-radius. I wanted rounded the corners for just the top corners. So I am going to do 0.3 ems space, 0.3 ems, space 0, space 0.

So that was top left, top right, and then the bottom two corners get 0. So I'll save that, preview that again, and now when I do a rollover, there is our tabs in all of their glory. About the only thing that's not going right for us right now is the red color is kind of distracting. I am going to go back into my hover, and I am going to change the background color from that red color, and I am going to make it the #916A31. Now that is color is that brown up top, so feel free to copy and paste it if you like.

I am going to go ahead and save that, go back to my menu, refresh, and there are our rollovers.. A really nice thing about this is the fact that those two colors are the same. It really makes it look like a nice seamless tab, and it looks like it's being tabbed up from the bottom of the bar which is the exact stylistic illusion that we were going for. I want to you to note how we're using the nav, the ul, and the links all together to form the finished styling. Now structuring your menus properly by using elements like nav and ul is going to give you a number of styling hooks.

So don't be afraid to use them when you're styling your menus. We're going to finish up our chapter next as we take a look at a method that allows us to indicate the current page.

Show transcript

This video is part of

Image for CSS: Styling Navigation
CSS: Styling Navigation

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