Presentable Accessibility Elements with CSS

By  on  

Mozilla is full of brilliant developers who the mass developer community doesn't know well enough.  Toward the top of that list is Craig Cook, a front-end web developer who works on Mozilla.org and other popular Mozilla sites.  One technique he recently brought to my attention is his creative handling of elements usually only thought of as helpers to screen readers.  Let me show you how we can make those hidden elements useful for all users.

The HTML

The HTML is simple, just a list of elements which link to different parts of the page:

<ul class="a11y-menu">
	<li><a href="#content">Skip to content</a></li>
	<li><a href="#search">Skip to search</a></li>
	<li><a href="#languages">Skip to languages</a></li>
</ul>

Create links to relevant popular and useful parts of the page, both for sighted and unsighted users.

The CSS

The first part of the CSS is simple and exactly what you'd expect:  simply hide the elements offscreen:

ul.a11y-menu {
	position: absolute;
	top: -500px;
}

The code above leaves the elements offscreen, so that the sighted user doesn't see them, but they're still tab-focusable so that screen readers can see them.  But looking at these links, wouldn't they also be useful to sighted users who prefer to use the keyboard?  Let's make that happen!

ul.a11y-menu a:focus {
	display: block;
	position: absolute;
	top: 520px; /* overcome the URL's 500px, then with some padding */
	left: 0;

	/* visual styles here, like padding, border, background, etc. */
}

While the entire list element stays out of site, any focused link is featured at the top of the screen so that it may be seen.

This post is more about the technique than the cleverness in code.  If you want to see this technique used in production, visit the Mozilla Blog; press the tab key a few times and you'll see the accessibility links display one by one in the top left side of the site.  This technique is excellent -- useful to the both the sighted and unsighted!

Recent Features

  • By
    5 More HTML5 APIs You Didn&#8217;t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    HTML5&#8217;s placeholder Attribute

    HTML5 has introduced many features to the browser;  some HTML-based, some in the form of JavaScript APIs, but all of them useful.  One of my favorites if the introduction of the placeholder attribute to INPUT elements.  The placeholder attribute shows text in a field until the...

  • By
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

Discussion

  1. DP

    Been using this technique for years, most recently here: http://target.chatelaine.com

  2. DP

    oops, i shall continue this thought…

    Nice to see other people use it and even better to have it published by someone who didn’t use it previously..

    Cheers.

  3. I think is better use position fixed if the object are relative of body, because the browser scroll to the focus object. (Have not tried)

  4. @DP: http://target.chatelaine.com is best demo, i thought

  5. This technique has been around for some time, but it’s always good to see things like this clearly documented and shared.

    One note is that too many skip links can be more of a hinderance than a help. Mouse users are oblivious to them, screen reader users can ignore them, but sighted keyboard users must tab through every single one.

    Three (as you have in your example) is probably about the maximum number of skip links you’d want at the top of a page. Otherwise it can be almost as awkward to tab through the skip links as it would be to tab through the content they’re intended to bypass.

  6. The implementation on the Mozilla blog is nice, I’ve been using a slightly more inelegant approach for a while but I’ve never gotten any real feedback on the effectiveness of it: http://jsfiddle.net/i_like_robots/zusHE/

  7. Nice way of accessibility, I would of done the same thing- thanks for sharing this :)

  8. Well, this technique is rather old. It is used in YAML since 2005: http://www.yaml.de/docs/index.html#yaml-accessibility

    Unfortunately it is rarely used.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!