Full answer
Tabs show one panel at a time. The ARIA tabs pattern has fixed parts.
- A container with
role="tablist"holds the tabs. Each tab is abuttonwithrole="tab". - Each tab has
aria-controlspointing to its panel andaria-selected="true"or"false". - Each panel has
role="tabpanel",aria-labelledbypointing to its tab, and thehiddenattribute when it is not selected. - Roving tabindex: only the selected tab has
tabindex="0". The others havetabindex="-1". Tab moves focus into the tab list once, then on to the panel. The arrow keys move between tabs.
Keyboard behaviour:
- Right arrow moves to the next tab, and wraps from the last to the first. Left arrow does the reverse.
- Home and End go to the first and last tab.
- With automatic activation, focus and selection move together. This is simple and good when panels show instantly. With manual activation, the arrows only move focus, and Enter or Space selects. This is better when a panel is slow to load.
When there is only a little content, headings and sections can be better than tabs, and an accordion is easier on small screens. Tabs hide content, so do not hide anything important.
Style the selected tab by [aria-selected="true"], so that the visual and the announced state are the same. Keep a visible focus style.
After focusing the first tab and pressing Right, the Syllabus tab is selected and its panel is shown. Pressing Right on the last tab wraps to the first.
Test with a screen reader too, since support for roles varies a little between readers.
<div role="tablist" aria-label="Course info" id="tl">
<button role="tab" id="t1" aria-controls="p1" aria-selected="true" tabindex="0">Overview</button>
<button role="tab" id="t2" aria-controls="p2" aria-selected="false" tabindex="-1">Syllabus</button>
<button role="tab" id="t3" aria-controls="p3" aria-selected="false" tabindex="-1">Reviews</button>
</div>
<div role="tabpanel" id="p1" aria-labelledby="t1">Overview text</div>
<div role="tabpanel" id="p2" aria-labelledby="t2" hidden>Syllabus text</div>
<div role="tabpanel" id="p3" aria-labelledby="t3" hidden>Reviews text</div>
<script>
const tabs = [...tl.querySelectorAll('[role=tab]')];
function select(t) {
tabs.forEach(x => {
const on = x === t;
x.setAttribute("aria-selected", on); x.tabIndex = on ? 0 : -1;
document.getElementById(x.getAttribute("aria-controls")).hidden = !on;
});
t.focus();
}
tl.addEventListener("click", e => { const t = e.target.closest("[role=tab]"); if (t) select(t); });
tl.addEventListener("keydown", e => {
const i = tabs.indexOf(document.activeElement); if (i < 0) return;
const to = { ArrowRight: (i + 1) % tabs.length, ArrowLeft: (i + tabs.length - 1) % tabs.length, Home: 0, End: tabs.length - 1 }[e.key];
if (to !== undefined) { e.preventDefault(); select(tabs[to]); }
});
</script>Checked with real key presses in Chrome. No screen reader was run. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- tablist, tab and tabpanel roles with aria-controls and aria-labelledby.
- Only the selected tab has tabindex 0.
- Arrow keys move between tabs; Home and End jump.
- Automatic or manual activation.
- Style from aria-selected.
Common mistakes
- Making every tab focusable with Tab, so users must tab through all tabs.
- Hiding panels with CSS only and forgetting aria-selected.
- Using div elements for tabs with no keyboard handling.
Follow-ups
- What is roving tabindex?
- When would you choose manual activation?
Tips
- Name the roles and the roving tabindex in the first sentences.
- Mention both automatic and manual activation.