Why accessibility matters
Web accessibility means designing and developing so that everyone can use our site. We need accessibility so people who use only a keyboard, people with visual impairments who use screen readers, and various assistive technologies can interpret our pages.
In practice, development raises questions: should every feature work without a mouse, using only a keyboard? Does a screen reader read my component properly? As AI-based screen readers and speech recognition technology improve, more people are using these assistive technologies. Components that ignore accessibility exclude them.
Improving accessibility also helps SEO. Semantic HTML and ARIA attributes help search engines understand page structure, and sites with good keyboard accessibility can also improve user experience metrics.
In this post, we’ll build an accessible tab component in React and explore WAI-ARIA attributes and keyboard navigation through practical code examples.
1. Using semantic tags
<Fragment>
<nav className="tabs">
<ul className="tab-navigation">
<li>
<input
type="radio"
id="tab-1"
/>
<label htmlFor="tab-1">
All
</label>
</li>
<li>
<input
type="radio"
id="tab-2"
/>
<label htmlFor="tab-2">
Web Design
</label>
</li>
<li>
<input
type="radio"
id="tab-3"
/>
<label htmlFor="tab-3">
Social Media
</label>
</li>
<li>
<input
type="radio"
id="tab-4"
/>
<label htmlFor="tab-4">
Creative
</label>
</li>
</ul>
</nav>
<section>
Tab content 1
</section>
<section>
Tab content 2
</section>
<section>
Tab content 3
</section>
<section>
Tab content 4
</section>
</Fragment>
- Building the tab component
<nav>: a container around the tab navigation that helps users move between sections of a page. Using<nav>instead of a div for navigation improves accessibility and meaning. Search engines and screen readers understand from this tag that the element relates to page navigation.<ul>and<li>: the tab menu consists of list items.<ul>(unordered list) indicates a list, and each tab is contained in an<li>. This makes the structural meaning clear, helping both maintenance and accessibility.<section>: each tab contains an independent content block, so I used<section>to clarify its meaning. It is useful for grouping related content and tells search engines and screen readers that each tab’s content is an independent section.input: strictly speaking, I used radio buttons rather than tabs. I chose form elements to make them keyboard accessible. We can communicate that this is a tab menu by adding roles below.label: linking a label’s htmlFor to an input’s id lets clicking the label select the radio button too.
- Also, it is better to use a Fragment than an unnecessary div with no meaning.
WAI-ARIA
WAI-ARIA describes the technologies needed to build accessible JavaScript sites.
JSX supports all aria-* HTML attributes.
-
Aria-selected: screen reader users need to know which tab is selected, so aria-selected uses true or false to identify the selected tab element.
-
Aria-label: use this when screen reader users need to know what a tab list is for. The tabs implemented here already have labels, so adding it is optional.
-
Aria-controls: when a child element affected by the selected element is far from the currently focused line, screen reader users may struggle to find where it opened. aria-controls helps them move directly to the related child element. Tabs can include it to move directly to the beginning of the panel when a particular tab is selected. It connects the tab to its panel.
-
Aria-labeled-by: an attribute that tells accessibility tools such as screen readers to refer to text in another element when describing an element. Simply put, it specifies that an element’s description should come from another element. When reading
tab-content-1, the screen reader describes it as “Tab 1,” letting users know that this content is associated with “Tab 1.” -
role: adding a role to a radio button explicitly identifies it as part of a tab component.
- Tab list: tabs and tab panels are grouped as a “tab list.” If a page has two tab sections, there are two tab lists.
- Tab: each tab element is a child of the tab list. If one tab list contains four selectable elements, it has four tabs. When a tab control is implemented with role=“tab”, a screen reader announces something like
“Accessibility blog tab, selected, 2 of 4,” telling users the total number of tabs and the position of the currently selected tab. - Tab panel: the content underneath each tab. Selecting a tab changes the related tab panel. Since one tab is always selected when the page first loads, one panel is also visible.
<!-- Tab --> <ul role='tablist'> <li> <input id="tab-1" type="radio" aria-controls="tabpanel-1" aria-selected={currentTab === 1} role="tab" /> <label htmlFor="tab-1" id="tab-txt1"> All </label> </li> <!-- Tab panel --> <div className="tab-content" id="tabpanel-1" role="tabpanel" aria-labelledby="tab-txt1" > Tab content 1 </div>
Form
HTML’s for must be written as htmlFor in React.
<label htmlFor="namedInput">Name:</label>
Controlling focus with the keyboard
Users should be able to access tabs freely with only a keyboard, without a mouse.
With the aria settings above, we can move between tabs with the arrow keys and move to the panel with the Tab key.
To make the interface clearer and improve the experience, let’s allow Enter to move to the tab panel.
- Use handleKeyDown so pressing Enter on a tab moves to its panel.
- tabIndex
- tabindex=“0”: how can we allow keyboard focus on a noninteractive element such as
<div>or<span>? Set itstabindexto0to let focus move to it as if it were interactive. - tabindex=“-1”: conversely, how can we prevent an ordinarily interactive element from receiving focus? Setting
tabindexto-1keeps focus from moving to it even if it is interactive. - Positive tabindex: by default, pressing
tabmoves focus in document order, whileshift+tabmoves in reverse. Settingtabindexto a positive value of 1 or more can forcibly change the focus order.
- tabindex=“0”: how can we allow keyboard focus on a noninteractive element such as
const handleKeyDown = (event, panelId) => {
// Move focus to the related tab panel when Enter is pressed.
if (event.key === "Enter") {
document.getElementById(panelId).focus();
}
};
<input
type="radio"
aria-controls="tabpanel-1"
aria-selected={currentTab === 1}
name="tab"
id="tab-1"
checked={currentTab === 1}
role="tab"
onChange={() => setCurrentTab(1)}
tabIndex="0"
onKeyDown={(e) => handleKeyDown(e, "tabpanel-1")}
/>
Testing it
- Chrome Screen Reader: you can run a screen reader yourself. (It starts speaking as soon as it is installed, which might startle you.) https://chromewebstore.google.com/detail/screen-reader/kgejglhpjiefppelpmljglcjbhoiplfn?hl=en
- React library: run the library and check the console for errors. Although I thought I’d been quite thorough, I found about seven errors.
https://github.com/dequelabs/axe-core-npm/tree/develop/packages/react
- Debugging the errors showed that they didn’t all seem to fit this particular tab component, so applying every recommendation may be difficult.
import("@axe-core/react").then(
({ default: axe }) => axe(React, ReactDOM, 1000, {}),
({ message }) => console.error(message)
);

- Lint: lint configuration can help improve accessibility during development.
https://github.com/infofarmer/eslint-plugin-jsx-a11y
Accessibility testing, which I’d expected to be difficult, turned out easier than I thought.
When developing quickly, it’s easy to overlook accessibility. This gave me an opportunity to address as much of it as possible.
Helpful resources
- National Standard Technical Guidelines for Improving Web Accessibility - Korean national technical guidelines established by the Ministry of the Interior and Safety.
- Web Accessibility Institute (WAH) - a site providing Korean web accessibility standards and guidelines, operated by the Ministry of Science and ICT and the National Information Society Agency.
- W3C WAI ARIA Authoring Practices Guide - Tabs Pattern
- Building CSS Tab Menus Is One Thing—Can They Be Accessible Too?
- React Official Documentation - Accessibility