Making your website accessible for keyboard-only users is an important part of the bigger accessibility picture. Here are some keyboard accessibility tips you can quickly implement using basic HTML and CSS.
What is Keyboard Accessibility?
A keyboard can be the primary means for some users to navigate websites. Nowadays, when most web interfaces are designed with mouse cursors and touch interaction in mind, keyboard navigation is neglected. Keyboard accessibility is the practice of making sure that users can navigate efficiently and unhindered using just their keyboard.
Who Might Need Keyboard Accessibility?
These are the main target groups of keyboard accessibility:
Users with motor disabilities who have difficulty using a mouse, using a touch device, or clicking on small things.
1. Test Your Site for Keyboard Accessibility
The most important goal of keyboard accessibility is to make every interactive element, such as links and form controls, available with the Tab key. This is how keyboard-only users navigate through a web page. Testing your website for keyboard accessibility is actually pretty easy: just press the Tab key and navigate from the top of the page to the bottom, highlighting active elements as you go.
Observe how easy or difficult it is to get to the main content, click a menu item, fill in a form, operate a slider, or follow your current position on the page. You can also test the reverse direction using the Shift + Tab keyboard shortcut.
2. Create Noticeable :focus Styles
CSS has a :focus pseudo-class that is triggered when a user clicks or taps on an item, or selects it with the Tab key. The :focus state only applies to focusable elements, namely a,button,input,textarea,select and area.
Every browser comes with its own default :focus styles–usually a dotted black outline around the element, or a blurred glow, however many designers find it not to their taste and will completely remove it. This is actually the number one mistake that ruins keyboard accessibility on web pages. If you don’t like the default styles, use something that matches your website’s design.
Choose a style that’s easily noticeable but doesn’t rely solely on colors.
3. Use Non-Color Designators for Links
Hyperlinks shouldn’t be distinguished only by color. This principle is usually mentioned in relation to visual accessibility, as people with low vision find the differences between certain colors difficult to discern. However, clearly visible links are also important for keyboard accessibility. Keyboard-only users should be able to spot links as quickly as possible. This helps them scan the page and figure out how to navigate to the part they are interested in.
Similarly to :focus styles, hyperlinks also come with default browser styling— blue underlines in most cases. However, designers frequently remove the underline and only use colors to indicate the presence of a link. If you remove the default underline always use another non-color designator that matches your website design, such as borders, icons, or outlines.
You can use the title attribute to describe a link’s content, but it only becomes visible when someone hovers the link. Keyboard-only users don’t have access to hover events, so never place crucial information in the title attribute. It also doesn’t count as a non-color designator. For example, never do this:
Instead, do this:
4. Use Native Control Elements
Forms are interactive elements, so they need to be accessible via the keyboard. Keyboard-only users should be able to fill in forms, press buttons, use range sliders, select options, and operate controls with ease. If you have any forms on your website you should test them one by one, using the Tab key. Think of signup forms, newsletter forms, login forms, comment forms, shopping carts, and so on.
The best way to create accessible forms is by using native control elements wherever it’s possible. Native control elements come with built-in keyboard accessibility, meaning they are focusable and respond to keypress events by default. They are as follows:
For example, you can create a keyboard accessible range slider with the following HTML:
Keyboard users can first focus it with the Tab key, then move the slider up and down with Space.
If you need to use a non-focusable HTML tag for an interactive element for some reason, you can make it focusable with the tabindex=”0″ attribute. For instance, here’s a turned into a focusable button:
The role=”button” attribute in the above snippet is an ARIA landmark role. Although keyboard-only users don’t need it, it’s indispensable for screen reader users and visual accessibility.
Even if the non-native button has been made focusable, it’s still inferior to its native counterpart in terms of keyboard accessibility. You’ll understand this immediately when you try to add an event handler to the button. Here’s what a click event listener looks like with the native
And, here’s the equivalent using the div button:
If you click the buttons using your mouse or touchpad you can see both alert messages. However, if you navigate to each button using the Tab key and hit Enter to process them, you will only see the first message, belonging to the native button. To make the non-native button process the keyboard input, you also need to define a keypress event handler separately:
Now, when keyboard users hit Enter, they also see the message belonging to the non-native button. As you can see, it’s much easier and quicker to use the native version. So, unless you have a good reason for not using them, always use native control elements.
5. Add a “Skip to Main Content” Link
Adding a Skip to main content or Skip navigation link to your web pages greatly helps keyboard-only users. In most cases, these users won’t want to jump through all the navigation links before they begin to read the content. This is especially true when they have a look at more than one page on your site. Just imagine, without a skip navigation link, they have to go through the same navigation links on the homepage every time. It doesn’t seem like a particularly entertaining activity.
To create a functioning skip navigation link, you need to bind it to the main content using the id and href HTML attributes in the following way:
You also need to add the tabindex=”-1″ attribute to the container of the main content. This is the same tabindex we have used above to make the non-native button focusable. The tabindex attribute is used to modify the default navigation order. With a value of 0, it makes non-focusable elements focusable. With a value of -1, it also makes elements focusable but they become non-reachable with default keyboard navigation. Certain browsers, such as Chrome and IE, require the presence of tabindex=”-1″ on the target of the skip navigation link, so never omit it.
Reveal the Skip Link Only to Keyboard Users
You can use CSS to make the skip navigation link visible only for keyboard users. In its default state, hide the link from regular users by positioning it out of the viewport. Then, reveal it for keyboard users by creating separate styles for the focus state that’s triggered when the user hits the Tab key.
In this article, We shared some basic keyboard accessibility tips which you can implement using HMTL and CSS. There are other, more advanced things you could do for keyboard accessibility as well. For instance, you could:
add custom keyboard shortcuts to your web app,
or build keyboard-accessible sliders.
In addition to these tips, avoid using CAPTCHAs where possible, as they have serious accessibility problems, both for screen reader and keyboard-only users. If you still need to use them, provide more than two ways to solve them, otherwise users with accessibility needs will struggle to process your forms.