Focus on the Hover

This is going to be the easiest website accessibility tip that anyone will ever give you:

Add the :focus psuedo-class to everything that has a :hover style in your CSS, so that keyboard users get the same visual goodness that you give people who use a mouse.

Any time I mention this, I’m always surprised at how many designers don’t realize that they can use this in their code. It doesn’t have any browser drawbacks, is incredibly easy to implement, and allows keyboard users the benefit of seeing all your nice hover effects as they tab through the page.

The inspiration to write this came about while I was looking at the Gowalla website. Most of the time, I use a mouse to navigate the links on a page. But sometimes I enjoy using a keyboard to navigate through my links, with a quick jab to the tab key. As I was reading their page, I grew concerned that I had no visual cue as to what link or button was currently and silently waiting for my return key. In other words, I was lost. Tab, tab, tab… where am I? Not a clue. I was quickly forced to switch over to my mouse, just so that I could ensure that the activated link was truly the one that I wanted.

This problem extends far beyond Gowalla though. There are countless amounts of websites that force people to use a mouse just to navigate the links. It’s a shame that it happens, considering the little amount of effort that it takes to resolve.

Forget something?

Back to Gowalla – upon further investigation of their CSS, I quickly discovered that they are using Eric Meyer’s reset.css script. Not surprising actually, considering that his reset has grown in popularity quite a bit since it was first launched.

He states clearly in the code, visible for all to read:


/* remember to define focus styles! */

I’m not exactly sure what the statistics are on the amount of people actually using Eric’s reset now, but I’m willing to bet that it’s a significant amount (probably more than I could even begin to guess). What would be more interesting though, would be to find out how many of those users have actually done the work to follow through with some sort of style after the reset has been applied as instructed. Any guesses? Please, don’t tell me - it’s probably more than I want to hear.

Piggyback Your Current Work

I do believe though, that a very high number of designers go to extreme lengths to style everything that has a :hover effect. From custom navigation to footer links. :hover is everywhere! So why not start with that? If you’ve got it in your CSS, then you’re already more than halfway there to fixing the problem!

Let’s be honest though: This isn’t going to fix any other accessibility issues your site might have. That’s where you should consult with a professional accessibility expert (such as Derek Featherstone, Glenda Sims, John F Croston III, Ann McMeekin, Joe Clark, or many others). My advice is just this: By adding a :focus style to every item that has a :hover, you’ll be off to a fantastic start to letting keyboard users navigate your website.

Here’s what I use for my links (disclaimer: I’m not currently using Eric’s reset, but the same principles apply - and it works):


a:hover, a:focus {
	color: yellow;
	text-shadow: 0 3px 5px rgb(0,0,0);
}

As you can see, I just piggybacked the a:focus immediately after a:hover. The two share the very same declarations. This allows the a tag to appear on a focus event (such as tabbing) as it does for a hover. You could give it it’s own line, but I’m just pointing out that you don’t have to.

You can see the same idea used on my top navigation:


#topnav #tn_art a:hover, #topnav #tn_art a:focus {
	background-position: -607px -120px;
}

With the above code, when I hit the tab key and give each link the focus it needs, it looks exactly the same as if I had just hovered over it with your mouse. Plus, I have the visual cue that tells me where my focus is on the page. Go ahead and try hitting tab a few times here on this page to see what I mean.

Go Forth, Evangelize

Once you are able to resolve your own website(s), I have one last request for you: Spread the word! Tell everyone you know (at least those who are afflicted by the issue) how to fix it. This will go a long way in improving the overall state of accessibility on the internet. From there, start thinking about accessibility more broadly, and consider looking for other things you might be able to improve.

Comments Back to Top

1. Keith Homel

Mar 16th, 2010

Good stuff. I’ve actually never used the keyboard to navigate, but I can see how this is very beneficial. Hence forth I will make sure to define my focus styles. :)

2. Tim

Mar 16th, 2010

I’m seeing more sites giving the option to navigate using the keyboard. And it is often extended past the native input / text / scroll navigation. One example is the new Crush Lovely site http://crushlovely.com/

As people do this I’m starting to see a lot of value in keyboard navigation. I, too, don’t use keyboard navigation often. I tend to use it when tabbing through inputs on forms. But I find myself trying to navigate more often now that it is becoming more common within trends.

3. Anton

Mar 16th, 2010

Actually Tim, as beautiful as the new Crush Lovely site is, it still doesn’t do what it should when it comes to tabbing through links as if you were hovering them. There’s focus happening, you just can’t *see* it.

The javascript navigation that detects the arrow keys is all well and good, but what’s it worth if they forgot the core level of basic keyboard navigation?

4. Jeff Smith

Mar 16th, 2010

Excellent article Anton, thanks for bringing this to people’s attention. Definitely something that I run into a lot in my day to day work, and drives me crazy when I can’t find my spot on the page when tabbing through links.

5. Tim

Mar 16th, 2010

Aye, I agree on that Anton. It has a major flaw, as you have pointed out. I was merely pointing out how sites like Crush Lovely have brought more focus upon keyboard navigation in recent, by use of JavaScript navigation. Which adds an ‘Oh wow’ factor. But yeah, I see what you mean. When I try to tab on their site I honestly have no idea what is going on, and then the page shifts. Very annoying.

You’d think the simple, easy things would be ironed out before moving towards the glossy features.

6. Shawn Grimes

Mar 16th, 2010

This is a very simple fix for that issue Anton. Glad you took the time to shed some light on it. However, what about users of Firefox 3.x for Mac? Apparently the tabbing through links functionality does not work. It only tabs through the Address bar, search bar, text inputs. I did some digging around via Google and found that if you…

Go to System Preferences -> Keyboard -> Keyboard shortcuts
Under Full Keyboard Access click the All Controls radio button

... you can then tab through links in FF 3.x for Mac and see all the hover states as they should be in all their glory.

Just wanted to share that little tidbit, thanks for the enlightening read good sir.

7. Anton

Mar 16th, 2010

Shawn, you’re totally right. Firefox on Mac doesn’t enable keyboard navigation by default (like Safari does). And even when it is active, it has that big blinking cursor next to the content.

8. Keith Homel

Mar 16th, 2010

Just another non-standard implementation to make our lives more difficult, especially when we are trying to keep hours VERY close to the vest for clients.

9. Derek Pennycuff

Mar 17th, 2010

This is a classic gotcha. Thanks to your reminder I just found 20 :hover styles without a corresponding :focus style. That was a quick fix.

10. Super web nerd!

Mar 19th, 2010

Awesome suggestion! I’ve used :focus before but never with the same intentionality as I plan on in the future!

11. Erken

Mar 19th, 2010

Thanks for the article! Always useful to work on accessibility. I’ll make sure to get the :focus after the :hover from now on ;)

12. alfin

Mar 20th, 2010

so,, people using tab for navigation nowadays?
i just knew that,, i thougt it’s only used to move between input box, then submit post..
this is a good article, learn somethin from here..

13. Steph

Mar 20th, 2010

Thanks for the reminder!  I tried pressing tab on your site, to get the effect.  It only worked on the form, so I don’t see what you mean :(

But I appreciate the reminder, and I must say: I LOVE YOUR SITE.  It’s gorgeous!  It’s really different.

14. yup

Mar 20th, 2010

nice

15. Anton

Mar 20th, 2010

I nearly forgot that I had to manually enable tabbing. I usually just do that when I install a browser, without really thinking about it. In Safari, it’s under the advanced preferences; “Press tab to highlight each item on a webpage”.

16. Maicon Sobczak

Mar 20th, 2010

Great tip. I did know about it.

17. Stephanie Sullivan

Mar 26th, 2010

I need to “check the latest” in regard to Internet Explorer 8, but I know in previous versions, IE treats :active like :focus (instead of using :focus). So I usually group the three together like this:
a:hover, a:active, a:focus { styles here } 
This way every user has an indication of where they are on the page whether mousing or tabbing. Thanks for writing this up Anton. It’s such a simple thing designers can do to add value to more users.

18. Anton

Mar 26th, 2010

Steph, that’s an excellent point, thank you!

19. Alan Houser

Mar 29th, 2010

I like this tip, and have NEVER considered it. I wonder how this will change with the iPad?

20. Martin Bean

Apr 17th, 2010

You know what? I’m gonna own up. I’ve read the ‘Remember to define focus styles’ comment in Eric Meyer’s CSS Reset when using it, but I’ve never actually followed up on the comment. This blog post has given me some food for thought.

21. educational grants

May 4th, 2010

Great, I never knew this, thanks.

22. isabellecruz

Jun 9th, 2010

Hello Anton, your works is excellent!!!