Keyboard Accessibility Tips Using HTML and CSS

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.

A11y From the Beginning

These tips are part of Web Accessibility: the Complete Learning Guide, where we’ve collected a range of tutorials, articles, courses, and ebooks, to help you understand web accessibility from the beginning.

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.
  • Blind or visually impaired users frequently prefer to navigate websites with specific Braille keyboards.
  • Amputees or those with congenital amputation (birth without a limb or limbs, specifically hands in this case) often use special hardware mimicking keyboard functionality.
  • Anyone who simply doesn’t have access to a functioning mouse or touchpad.

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.

Navigating quick links in the Tuts footer with the Tab key
Navigating quick links in the Tuts+ footer with the Tab key

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. 

focus styles in Google Chrome browser
Default :focus styles in Google Chrome browser

Choose a style that’s easily noticeable but doesn’t rely solely on colors. Here’s a possible example that can work well for keyboard-only users:

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.

Clear links on govuk 1 hyperlink 2 focused 3 visited
Clear links on gov.uk: (1) hyperlink (2) focused (3) visited

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:

WCAG 2.0 also warns about the accessibility problems of the title attribute. Either use it with care or don’t use it at all.

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:

  • <button>
  • <input>
  • <textarea>
  • <select>
  • <option>

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 <div> 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 <button> element:

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. 

You can see this effect in action on sites like webaim.orgwww.w3.org, and (as usual) www.gov.uk:

Reveal the Skip Link Only to Keyboard Users
Have you ever seen this link before?

The following CSS is a simplified version of the skip navigation code of NC State University’s IT Accessibility Handbook:

When the user hits the Tab key, the .skip-main element is given its focus state and the skip navigation link appears on the screen. 

You can quickly test how it works if you click on the very top of the demo below and hit the Tab key. You may find it easier to open the demo below in full page view instead.

Next Steps

In this article, I 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:

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. Let us know if you have any keyboard accessibility tips of your own!