Accessible tabs let visitors switch between related content panels using a keyboard, pointer, or assistive technology. A tab that changes color on click can still fail when its selected state is missing, arrow keys do nothing, or hidden panel links remain in the keyboard path.
This guide builds a small, horizontal, single-selection tab set for a project page. It uses manual activation: arrow keys move focus, while Enter or Space selects the focused tab. The implementation and test form one reviewable example you can adapt before deploying a portfolio, documentation page, or product demo.
Accessible tabs launch checklist
- Confirm the content needs panels on one page rather than links to separate routes.
- Give the tab list a useful name and every tab a unique panel relationship.
- Keep exactly one selected tab and one displayed panel.
- Separate keyboard focus from selection when using manual activation.
- Test entry, arrow navigation, activation, and exit without a pointer.
- Review narrow layouts, zoom, and screen-reader announcements.
- Save a component regression test, then check the surrounding public page.
Use ordinary links for site navigation. A row of links to separate project pages should retain link behavior, including opening in another browser tab. For expandable FAQ answers, consider the native HTML details element. Choose this tab pattern when switching views of a single grouped section makes the task easier.
Connect ARIA tabs to their panels
Start with two buttons and two panels. Put the markup in a page where the initialization script below runs after these elements exist:
<div data-project-tabs>
<div role="tablist" aria-label="Project information">
<button type="button" role="tab" id="overview-tab"
aria-controls="overview-panel"
aria-selected="true" tabindex="0">Overview</button>
<button type="button" role="tab" id="setup-tab"
aria-controls="setup-panel"
aria-selected="false" tabindex="-1">Setup</button>
</div>
<div role="tabpanel" id="overview-panel"
aria-labelledby="overview-tab" tabindex="0">
<p>Review the project purpose and supported environments.</p>
</div>
<div role="tabpanel" id="setup-panel"
aria-labelledby="setup-tab" tabindex="0" hidden>
<p>Install the dependencies, then configure the project.</p>
</div>
</div>
The button's aria-controls points to its panel, and the panel's aria-labelledby points back to the button. Reuse neither ID elsewhere on the page. The visible button text supplies the tab name; keep it short and specific. See the accessible name guide when an icon or extra ARIA label changes the announced text.
MDN's tab role reference explains the tab, tablist, and tabpanel relationship. Roles describe the widget; they do not add its keyboard behavior. In this example, type="button" also prevents a tab from submitting an enclosing form.
The panels begin with text, so both have tabindex="0". Once activated, the visible panel becomes the next Tab stop. The hidden panel and its descendants should stay outside the interaction path. Do not replace hidden with opacity or an off-screen transform, which can leave invisible controls reachable. Check that application CSS does not override the hidden state.
This compact example assumes successful JavaScript initialization. For a public documentation page that must work without JavaScript, render readable sections first and apply tab roles, hiding, and event handlers together during enhancement. An uninitialized tab interface with hidden instructions leaves visitors unable to reach that content.
Add manual tablist keyboard navigation
Use the W3C tabs pattern as the interaction reference. Horizontal tabs use Left and Right Arrow to move within the list; Tab leaves the list. Enter or Space activates a manually selected tab. Home and End are useful optional shortcuts. Keep Up and Down Arrow available for normal page scrolling in this horizontal example.
Load this JavaScript as a module after the markup. It is scoped to one fixed tab set with no nested tab widgets:
const root = document.querySelector("[data-project-tabs]");
if (!root) throw new Error("Project tabs were not found");
const tabs = [...root.querySelectorAll('[role="tab"]')];
const panels = tabs.map((tab) => {
const panel = document.getElementById(tab.getAttribute("aria-controls"));
if (!panel || !root.contains(panel)) {
throw new Error("A project tab has no local panel");
}
return panel;
});
function selectTab(selected) {
tabs.forEach((tab, index) => {
const active = tab === selected;
tab.setAttribute("aria-selected", String(active));
tab.tabIndex = active ? 0 : -1;
panels[index].hidden = !active;
});
}
tabs.forEach((tab, index) => {
tab.addEventListener("click", () => {
selectTab(tab);
tab.focus();
});
tab.addEventListener("keydown", (event) => {
let next;
if (event.key === "ArrowRight") next = (index + 1) % tabs.length;
if (event.key === "ArrowLeft") next = (index - 1 + tabs.length) % tabs.length;
if (event.key === "Home") next = 0;
if (event.key === "End") next = tabs.length - 1;
if (next === undefined) return;
event.preventDefault();
tabs[next].focus();
});
});
Arrow navigation deliberately leaves selection and tabindex unchanged. Focus can move programmatically to a button with tabindex="-1"; the selected tab remains the single entry point when returning to the group. Enter and Space use the native button's click activation, which calls selectTab. Adding another key handler that toggles selection can process the same action twice.
For example, start with Overview selected. Right Arrow focuses Setup while Overview remains visible. Space selects Setup and reveals its panel. Tab then enters that panel. This distinction matters when loading a panel could be slow. W3C's manual activation example discusses the choice and the need to test assistive technology support. Automatic activation is an alternative for panels that appear without noticeable delay; update the behavior and test expectations together if you choose it.
For a framework component, keep the selected index in one state owner and derive panel visibility and aria-selected from it. Avoid independent state variables for each attribute. Dynamic removal, disabled tabs, nested widgets, vertical orientation, and right-to-left layouts need additional behavior and tests beyond this fixed example.
Make focus and selection separately visible
A selected tab identifies the displayed panel. A focus indicator identifies the control that receives the next keyboard action. During manual activation, these can be different buttons.
[data-project-tabs] [role="tablist"] {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
}
[data-project-tabs] [role="tab"] {
padding: 0.75rem 1rem;
border: 2px solid currentColor;
border-radius: 0.25rem;
}
[data-project-tabs] [role="tab"][aria-selected="true"] {
text-decoration: underline;
text-decoration-thickness: 0.2em;
text-underline-offset: 0.25em;
}
[data-project-tabs] :focus-visible {
outline: 3px solid currentColor;
outline-offset: 3px;
}
These are starting styles for the sample, not a complete theme. Test the actual foreground, background, borders, and outline in your deployed design, including forced colors. A pointer-selected tab should still look selected when keyboard focus moves elsewhere.
With long translated labels or increased text size, check that the list remains understandable as it wraps. If the design changes to vertical tabs, update orientation and keyboard handling together. For a short horizontal list, keep the number of choices small enough that wrapped rows do not make Left and Right movement confusing.
Add focused accessible tabs testing to CI
Save this in an existing Playwright Test project. Set PREVIEW_URL to the deployed page containing the exact sample markup and script. The labels and IDs are a fixture contract; replace them when adapting the test to your component.
import { test, expect } from "@playwright/test";
test("project tabs support manual keyboard activation", async ({ page }) => {
const target = process.env.PREVIEW_URL;
if (!target) throw new Error("PREVIEW_URL is required");
await page.goto(target);
const list = page.getByRole("tablist", { name: "Project information" });
const overview = list.getByRole("tab", { name: "Overview", exact: true });
const setup = list.getByRole("tab", { name: "Setup", exact: true });
const firstPanel = page.locator("#overview-panel");
const secondPanel = page.locator("#setup-panel");
await expect(list.getByRole("tab", { selected: true })).toHaveCount(1);
await expect(overview).toHaveAttribute("aria-controls", "overview-panel");
await expect(setup).toHaveAttribute("aria-controls", "setup-panel");
await expect(firstPanel).toHaveAttribute("aria-labelledby", "overview-tab");
await expect(secondPanel).toHaveAttribute("aria-labelledby", "setup-tab");
await overview.focus();
await overview.press("ArrowRight");
await expect(setup).toBeFocused();
await expect(overview).toHaveAttribute("aria-selected", "true");
await expect(secondPanel).toBeHidden();
await setup.press("Space");
await expect(setup).toHaveAttribute("aria-selected", "true");
await expect(overview).toHaveAttribute("tabindex", "-1");
await expect(setup).toHaveAttribute("tabindex", "0");
await expect(firstPanel).toBeHidden();
await expect(secondPanel).toBeVisible();
await setup.press("Tab");
await expect(secondPanel).toBeFocused();
await page.keyboard.press("Shift+Tab");
await expect(setup).toBeFocused();
await setup.press("ArrowRight");
await expect(overview).toBeFocused();
await overview.press("Enter");
await expect(overview).toHaveAttribute("aria-selected", "true");
await expect(firstPanel).toBeVisible();
await expect(secondPanel).toBeHidden();
});
Playwright's assertion documentation describes the retrying focus, visibility, and attribute checks used here. There are no fixed sleeps: each assertion waits for its expected state. Save a trace on failure so a developer can inspect focus and attributes at the failing step.
The test starts by focusing Overview to isolate the widget contract. It does not prove that visitors can reach the widget from the address bar. Extend it with your page's real entry path, Left Arrow wrapping, Home and End, pointer selection, and another tab set if your page uses several. Use the website regression testing guide to keep those expectations tied to a known release.
Review announcements and the surrounding public page
Complete a separate manual pass in supported browser and screen-reader combinations. Confirm the group name, tab labels, selected state, and panel name are understandable. Move into panel content, follow any links, and return to the tab list. Repeat at a narrow width and with zoom. Record the first failed action, browser, assistive technology, viewport, and build revision; avoid filing only “tabs inaccessible.”
Use the keyboard accessibility testing checklist for the complete page path. A passing component test still leaves headers, overlays, forms, and footer controls to review.
After the tab interaction works, run Slop Audit on the public page. When its browser stage is available, review the observed axe findings, bounded keyboard traversal, visible focus evidence, and mobile layout findings. Fix concrete failures and rescan the same route.
Slop Audit does not perform this tab-specific arrow-key and panel-selection test, activate every hidden panel, or verify screen-reader announcements. Its page-level evidence complements the focused checks above. Keep both the component test and the manual result with your release review so an unchanged audit score cannot hide a broken tab interaction.