Keyboard support is one of the most important accessibility parts that every application should include. We must ensure every user interaction including people with disabilities. It is important that every part of our application can be used through the keyboard as well.
In previous blog articles, we got acquainted with accessibility standards and how to design and develop with accessibility in mind. In my last blog article in the series of digital accessibility, I wanted to discuss keyboard support for our application.
Types of Disabilities
Our application should be operable by keyboard users. Who are keyboard users?
- Persons with physical disabilities who have limitations in the use of their arms. They use a specially designed keyboard, but can't use a mouse. They don't use any other assistive technology and can't use a website if they need a mouse.
- Persons who are legally blind. They use a screen reader to browse a website. It's a text to speech program that reads out the content of the website. They operate websites using the keyboard alone as they can't see the screen and use a mouse.
- Persons who use a mouse with lowered hand dexterity. They find it difficult to click on small items or may accidentally click the wrong item if buttons are put too close together. They struggle with the use of mouse and keyboard more.
- Persons with situation disabilities. In cases of e.g. arm injury, you may use the keyboard more than usual, or you are trying to shop online while eating. There are a lot of reasons people do not use a keyboard.
When you operate a website with a keyword, there are just a few basic commands that you need to know. There are various elements on the page that could be operated like input controls, links. These are called focusable elements. Pressing TAB on the keyboard moves the keyboard focus to the next focus element, pressing SHIFT + TAB moves backwards. Pressing the TAB through the IM site, you can see the slight outline showing the time moving. To move back press SHIFT and TAB. To navigate to the link in the menu item, press ENTER. It will activate a link or submit a form.
||Move between focusable elements
||Back to previous focusable element
||Submit a form or active link
||Toggle a checkbox or open a select control.
||Scroll up and down on the page
Keyboard Focus Visibility
When tabbing through different elements of the page with keyboard focus, a color outline is needed. Without the outline the user will not see what the element is focused on and it will be impossible to browse a site. It’s like using a mouse without seeing the cursor.
Focus should be improved using a focus selector.
// styles here
The default focus works well in most cases. The only occasion where you could change it is if you have a background or color that is a similar shade of color and the outline is not clear. There are a few techniques how you can improve focus indicators:
- Change the color of the outline
- Change the color of the entire element
- Style of hyperlinks can be improved. Default outline around links can look a bit clunky.
Using focus within you can make the form itself stand out visually in addition to style applied to the individual focused input. This can help the perceptibility of focus overall.
border: 1px solid blue;
Keyboard Focus Order
WCAG 2.3.4 Focus order-level A criteria – Focus elements must receive focus in an order that preserves meaning and operability. Focus of the element should flow in a way that users expect. We should avoid jumping between sections.
For example, users expect to read the page from left to right end from top to bottom. Users will expect the first focus element to be the logo then the navigation menu.
Using tabindext attribute you can control keyboard focus.
Tab index can have 3 values:
- -1 – Element is not in the tab order.
- 0 – You can tab to an element but the focus order is determined by the order of the HTML.
- 1+ - Any positive number indicates that you can tab to an element and the focus order is determined by the value of the attribute. Can be between 1 and 32.767.
Every application consists of different forms where we request an engagement. Forms should be clear, logical and easy to use.
Labelling a Form
Screen readers have forms mode where there is read out text directly associated with an input element. It’s important to name those properties in a proper way.
With each input element, we need to associate the corresponding name. The best way is to use label elements along with the input field. Labels are preferred because they solve two problems at once, the name of the element rendered visually and the name of the element in the accessibility tree. If we use labels and input elements those elements need to be connected.
This example doesn’t connect those elements. There are two ways to solve this problem:
- Wrap the input inside the label.
- Set input ID and use for attribute in the label.
<input type="password" id="password">
Another great usability improvement with this approach is if you can click to label in order to select a field.
Sometimes there is a need for an input field without a form label. One of the most common scenarios is search input inside the navigation header. Adding aria-label attribute screen readers will read out while this attribute will not be visible.
<input type="search" aria-label="Search" id="search">
If there is an input field along with the button you can associate input with button text in the following way:
<input type="search" aria-describedby="btn-search" id="search">
Screen readers only see the labels and if you need to provide additional text or description for the input element you need to add an additional aria attribute.
<input type="password" id="password" aria-describedby="password-rule">
<span id="password-rule">Must be at least 3 characters long</span>
Now the screen reader will read the label and additional description described by the associated element.
Disabled vs Read-only Fields
When you use disabled or read-only field you have to use the correct method to meet WCAG. Adding disabled or read-only attributes makes a special type of field which doesn’t take a value. Difference between disable and read-only field:
|Can't click into field
||Can't click into field
|Can't edit the value
||Can't edit the value
|Value not submitted to server
||Value is submitted to server
|Don't receive keyboard focus
||Receive keyboard focus
HTML 5 introduced a lot of new types of form input such as search, email, date etc. They can help in adding context for users and improve the experience. A different keyboard will be displayed on mobile when entering a telephone number versus an email.
You should always state which fields are optional and which are required. It could be a very bad experience if the user has to guess validation and after submitting the form to come back and re-enter data. Using an asterisk, the user sees which fields are required. Screen readers don’t recognize asterisk as a required field and a note in the HTML needs to be added.
Adding required attributes would cause the screen reader to announce it as a required field. Another good advantage is that browsers will pick up attributes and add validation with a little popup message.
<input type="text" required>
<input type="text" aria-required="true">
Now screen readers will read that out as required field:
If you hit the submit button or populate the input field with wrong data additional validation messages will appear.
<input type="password" id="password" aria-required="true">
<span>This filed is required.</span>
The main problem is that screen readers are not informed of this validation message since this validation is not linked to the field in any way.
Adding the aria-invalid attribute will inform the screen reader that the field is not valid. Using aria-describedby will read out associate error messages so that the user knows what the issue is.
<input type="password" aria-required="true" aria-invalid="true" aria-describedby="password-rule"><span id="password-rule">This filed is required.</span>
Adding a validation summary on the top of the form makes it much clearer when errors occur and guide the user towards those errors. When the user hits the submit button and if issues are found, the script will set keyboard focus to the validation summary. This jumps the user right back to the start of the form to explain there is a problem.
Status messages must be identified programmatically so that assistive technologies can announce them. This rule is according to WCAG 4.1.3 Status Messages – Level AA criteria.
There are two ways that you can announce a status message.
- Set keyboard focus to the validation summary. Keyboard focus will bring screen user attention to the issues.
- Use live region recommended in WCAG 4.1 which uses aria attribute to mark validation summary live region. Live region announces messages to the user even if they don’t receive keyboard focus. Adding the role attribute of alert informs the screen reader that they need to pay attention to this element because all changes need to be announced to the user.
<div id="validation-summary" role="alert"></div>
This informs screen readers that they need to pay attention to this element because all changes need to be announced.
In this blog article, we have seen how we can make our application keyboard accessible. How to design and validate form when users interact with it. A form is a complex element that manages a lot of information and we have to make sure that everything is clear and understandable for all users.
Following this guide makes your application supported by keyboard and accessible for screen readers and ensures that users with or without a disability have a good user experience.