Accessible tabs
Learn accessible tabs for web components: the ARIA tabs pattern, roles and states from ElementInternals, vertical and manual keys, and screen reader tests.
- 01Audit tabs against the ARIA patternCheck every role, state, name, and key the APG asks for, and prove each one in Chrome's accessibility tree.
- 02Give tabs their semantics from internalsBuild
tea-tabandtea-tab-panel, whose roles and states live in ElementInternals, and wire them with element references. - 03Support every keyboard, and test itAdd vertical tabs, right-to-left keys, and manual activation, then check them with VoiceOver, NVDA, and Chrome's tree.
How do you make accessible tabs in a web component?
Leaf and Kettle shows each tea's facts in tabs: Taste, Brewing, and Origin. Meena uses a screen reader. She presses Tab to reach the Sencha tabs, and her screen reader tells her where she is. What does it need to know?
Accessible tabs follow the ARIA tabs pattern. A tab list holds the tabs. One tab is selected, and each tab controls a panel that it names. Arrow keys move along the tabs, and Tab goes in at the selected tab. In a web component, the roles and states can live in each element's ElementInternals. Then the page's markup stays its own.
The kit's tabs from lesson 10.2 already do most of this. This lesson checks them against the pattern and closes the gaps. Here is where they end up:
<h3 id="sencha">Sencha</h3><tea-tabs labelledby="sencha"> <tea-tab slot="tab">Taste</tea-tab> <tea-tab slot="tab">Brewing</tea-tab> <tea-tab slot="tab">Origin</tea-tab> <tea-tab-panel slot="panel">Fresh and grassy.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel> <tea-tab-panel slot="panel">Grown in Japan.</tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; const tabs = document.querySelector("tea-tabs"); tabs.addEventListener("tea-change", (event) => console.log("Showing:", event.tab.textContent));</script>- Line 1 is the heading you see. Line 2 names the tab list after it, with a new attribute, labelledby.
- Lines 3 to 5 are three
tea-tabelements. Atea-tabis a tab that keeps its role and its state inside itself. You'll build it below. - Lines 6 to 8 are three
tea-tab-panelelements, one for each tab, in the same order. - Line 13 prints the tab a person picks. The
tea-changeevent comes from lesson 10.2.
Click Brewing. It prints Showing: Brewing. Press Right Arrow: Showing: Origin. Press Tab, and focus moves into the Origin panel.
The page has no role and no aria- attribute. Yet Chrome's accessibility tree has every role. That tree is the version of the page that screen readers read, as lesson 13.1 showed:
heading "Sencha"
tablist "Sencha", orientation: horizontal
tab "Taste", selected: true
tab "Brewing", selected: false
tab "Origin", selected: false
tabpanel "Taste"The tab list is named Sencha, after the heading. Taste is selected. The one panel you can see is named Taste, after its tab.
The tea shop keeps its recipes in a binder with dividers: Taste, Brewing, and Origin. You open the binder at one divider and read the pages behind it. The word on the divider tells you what those pages are about.
- In real life: The row of dividers along the edge
- In JavaScript: The tab list
- In real life: One divider
- In JavaScript: One tab
- In real life: The pages behind a divider
- In JavaScript: Its tab panel
- In real life: The divider the binder is open at
- In JavaScript: The tab with
aria-selected="true" - In real life: The word printed on the divider
- In JavaScript: The tab's name, which also names its panel
Accessible tabs are a tab list of tabs, one selected, each controlling a panel it names. The arrow keys move along the tabs. In a web component, the tabs and panels can take their roles and states from ElementInternals, and point at each other with element references.
Why it matters: a screen reader can only describe what the tree holds. With the right roles, Meena hears that she's on a tab, which one, and whether it's picked.
What does the ARIA tabs pattern require?
The ARIA Authoring Practices Guide, the APG, has a page for each common widget. Its tabs pattern lists the roles, the states, and the keys. Here are the kit's tabs as lesson 10.2 left them, with buttons and paragraphs:
<tea-tabs label="Sencha"> <button slot="tab">Taste</button> <button slot="tab">Brewing</button> <button slot="tab">Origin</button> <p slot="panel">Fresh and grassy.</p> <p slot="panel">Steep for 2 minutes.</p> <p slot="panel">Grown in Japan.</p></tea-tabs><script type="module"> import "./tea-kit/index.js";</script>- Line 1 names the row with the
labelattribute. - Lines 2 to 4 are plain buttons. Lines 5 to 7 are plain paragraphs.
- Line 10 loads the kit as it was after lesson 14.2.
tablist "Sencha", orientation: horizontal
tab "Taste", selected: true
tab "Brewing", selected: false
tab "Origin", selected: false
tabpanel "Taste"It's the same tree as the overview's, without the heading. Read each row of the audit across:
| What the APG asks | Lesson 10.2's tabs | What Chrome's tree shows |
|---|---|---|
| The box around the tabs has the role tablist. | Yes. The row in the shadow root has it. | A tab list, horizontal. |
| Each tab has the role tab, inside the tab list. | Yes. Each button gets role="tab". | Three tabs inside the tab list. |
| Each panel has the role tabpanel. | Yes. Each panel gets role="tabpanel". | One tab panel. Hidden panels aren't in the tree. |
The selected tab has aria-selected="true", the others "false". | Yes. | Taste is selected. Brewing and Origin are not. |
Each tab points at its panel with aria-controls. | Yes, by id. | Taste controls the Taste panel. |
Each panel is named by its tab with aria-labelledby. | Yes, by id. | The panel is named Taste. |
The tab list's name comes from its visible heading, or else from aria-label. | Only aria-label, from the label attribute. It can't point at a heading. | Named Sencha, by aria-label. |
A vertical tab list has aria-orientation="vertical". | No. The tabs can only run across. | Always horizontal. |
Chrome lists the links between them too. Taste's row says it controls the Taste panel. The panel's row says it's labelled by Taste. A tab list is horizontal unless it says otherwise, as WAI-ARIA defines it.
One rule is argued about. Chrome's own tabs guide says no screen reader uses aria-controls, and leaves it out. The APG still asks for it, so the kit keeps it.
Which keys does the pattern ask for?
- Tab enters the tab list at the selected tab, then goes on to its panel. The kit does this.
- In a row, Left and Right Arrow move to the tab before or after, around the ends. The kit does this. Up and Down keep scrolling the page.
- In a column, Up and Down Arrow do the same job. The kit can't make a column.
- Enter or Space picks the focused tab, when focus didn't pick it. The kit's buttons click on those keys. A tab that isn't a button wouldn't.
- Home and End go to the first and last tab. They're optional, and the kit has them.
- Delete may close a tab, if closing is allowed. That's optional too.
Lesson 14.2 added one more rule: on a right-to-left page, Left Arrow moves forward. The kit's tabs don't swap the keys yet. So the audit finds four gaps:
- The tab list can't be named by the heading you see.
- The tabs can't run down a column.
- There's no manual activation, and Left and Right don't swap right to left.
- The tabs write ids and ARIA attributes on the page's own buttons.
Why it matters: an audit turns "accessible" into a list you can check, row by row, in Chrome's tree.
How do you set tab roles and states with ElementInternals?
Start with the fourth gap. What do the kit's tabs write on the page's markup? Try both kinds:
<tea-tabs label="Sencha"> <button slot="tab">Taste</button> <button slot="tab">Brewing</button> <button slot="tab">Origin</button> <div slot="panel">Fresh and grassy.</div> <div slot="panel">Steep for 2 minutes.</div> <div slot="panel">Grown in Japan.</div></tea-tabs><script type="module"> import "./tea-kit/index.js"; const tabs = document.querySelector("tea-tabs"); console.log(tabs.children[1].outerHTML); console.log(tabs.children[4].outerHTML);</script>tablist "Sencha", orientation: horizontal
tab "Taste", selected: true
tab "Brewing", selected: false
tab "Origin", selected: false
tabpanel "Taste"The tabs give Brewing a made-up id, a role, aria-selected, and aria-controls. They give its panel an id, a role, and aria-labelledby. They write them again on every draw, over anything the page wrote.
- Lines 2 to 7 are the tabs and the panels.
- Line 12 prints the Brewing tab's markup. Line 13 prints the markup of its panel.
- Buttons and divs: the button comes back with
id="tea-tab-2",role="tab",aria-selected, andaria-controls. Its panel gets an id, a role, andaria-labelledby. - tea-tab and tea-tab-panel:
<tea-tab slot="tab" tabindex="-1">. Nothing else is added.
Chrome's tree is the same both ways. The difference is who owns the markup. The kit writes on the page's buttons on every draw. If the page wrote its own aria-controls, the kit would write over it.
Lesson 13.2 showed a better home for a role. A custom element asks for its ElementInternals with attachInternals(). Its role and aria properties set the element's default semantics. The HTML Standard says they're used unless the page writes an attribute.
But internals only speak for their own element. The tabs can't give the page's button a role that way. So the kit gets two small elements of its own. Here is the tab's constructor:
constructor() {
super();
this.#internals.role = "tab";
this.#internals.ariaSelected = "false";
this.addEventListener("keydown", (event) => {
if (event.key !== "Enter" && event.key !== " ") return;
event.preventDefault();
this.click();
});
}- Line 3 sets the role tab. Line 4 sets the starting state: not selected.
- Lines 5 to 9 turn Enter and Space into a click, as a button does. Line 7 stops Space from scrolling the page.
Then two properties, which tea-tabs sets each time it draws:
set selected(value) {
const on = Boolean(value);
this.#internals.ariaSelected = String(on);
if (on) this.#internals.states.add("selected");
else this.#internals.states.delete("selected");
}
/** The panel this tab shows. tea-tabs sets it. */
get panel() {
return this.#internals.ariaControlsElements?.[0] ?? null;
}
set panel(element) {
this.#internals.ariaControlsElements = element ? [element] : null;
}- Line 3 sets
ariaSelected, the state a screen reader hears. - Lines 4 and 5 add or remove the
selectedcustom state. CSS matches it as:state(selected), and the kit makes that tab bold. - Lines 13 to 15 hand the panel element itself to
ariaControlsElements. No id is needed. Lines 9 to 11 read it back.
tea-tab-panel is even smaller. Its constructor sets the role tabpanel. Its tab property sets ariaLabelledByElements, so the panel is named by its tab. MDN lists every ARIA property that internals have.
Both elements also run lesson 4.2's upgrade-property pattern. The kit's index loads tea-tabs.js before tea-tab.js. So the tabs may set selected before a tab is upgraded, and the pattern keeps that value.
Inside tea-tabs, each draw now goes one of two ways:
this.#tabs().forEach((tab, index) => {
const panel = panels[index];
if (!panel) return;
const selected = index === selectedIndex;
if (tab.localName === "tea-tab" && panel.localName === "tea-tab-panel") {
tab.selected = selected;
tab.panel = panel;
panel.tab = tab;
} else {
if (!tab.id) tab.id = `tea-tab-${nextId++}`;
if (!panel.id) panel.id = `${tab.id}-panel`;
tab.setAttribute("role", "tab");
tab.setAttribute("aria-selected", String(selected));
tab.setAttribute("aria-controls", panel.id);
panel.setAttribute("role", "tabpanel");
panel.setAttribute("aria-labelledby", tab.id);
}
tab.tabIndex = selected ? 0 : -1;
if (!panel.hasAttribute("tabindex")) panel.tabIndex = 0;
panel.hidden = !selected;
});- Line 5 checks for a
tea-taband atea-tab-panel. Lines 6 to 8 set their properties. - Lines 10 to 16 are lesson 10.1's way, for any other pair, like a button and a div. Older pages keep working.
- Lines 18 to 20 run for both. The roving tabindex and
hiddenstay attributes. No internals property sets them.
Leaf and Kettle's aprons have the shop's name sewn on. Nobody sticks a label on them each morning. The shop never puts stickers on a guest's own coat. If a guest pins on a badge, everyone reads the badge.
- In real life: A staff apron with the name sewn on
- In JavaScript: A
tea-tabwhose role lives in its internals - In real life: A sticker on a guest's own coat
- In JavaScript: A
roleattribute the kit writes on the page's button - In real life: A badge the guest pins on
- In JavaScript: An attribute the page writes, which wins
The page can still override, as lesson 13.2 showed. In the overview, change line 4 to <tea-tab slot="tab" aria-selected="true">Brewing</tea-tab>. Chrome's tree then shows two selected tabs. The page wins, even when it's wrong.
Why is the role on the tea-tab itself, and not on a button in its own shadow root? The tea-tab is what sits in the tab slot. It takes focus, and the arrow keys move between the tabs. Lesson 14.1 showed what goes wrong when a host has the role but focus lands inside. Chrome's web components guide says the same.
Google's HowTo: Components built tabs this way long ago, with howto-tab and howto-panel. They wrote role attributes on themselves. Internals now do that job without touching the markup.
How do tabs and panels point at each other across shadow DOM?
Ids only work inside one tree. Lesson 10.1 showed that a tab in the page can't find a panel inside a shadow root by id. Lesson 13.3 showed the way out. An element reference, set with a property like ariaLabelledByElements, can point out of a shadow root. It can never point in.
The kit's tabs have one link that crosses. The tab list is the row inside the shadow root. The heading Sencha is in the page. The APG says a tab list with a visible label should be named by it. Try three ways:
<h3 id="sencha">Sencha</h3><tea-tabs label="Sencha"> <tea-tab slot="tab">Taste</tea-tab> <tea-tab slot="tab">Brewing</tea-tab> <tea-tab-panel slot="panel">Fresh and grassy.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; const row = document.querySelector("tea-tabs").shadowRoot.querySelector('[role="tablist"]'); console.log("aria-label:", row.ariaLabel); console.log("Labelled by:", row.ariaLabelledByElements?.[0]?.textContent ?? "nothing");</script>heading "Sencha"
tablist "Sencha", orientation: horizontal
tab "Taste", selected: true
tab "Brewing", selected: false
tabpanel "Taste"The tabs copy the text to the row's aria-label. The tab list is named Sencha, but the name isn't tied to the heading. Change the heading, and the name stays Sencha.
- Line 1 is the heading. Line 2 is the only line that changes.
- Line 10 finds the row inside the tabs' shadow root. Line 11 prints its
aria-label. Line 12 prints the text of the element it's labelled by.
- label="Sencha" prints
aria-label: Sencha. The name is right, but it's typed twice. Change the heading, and the name stays Sencha. - aria-labelledby="sencha" prints
Labelled by: nothing. The attribute names thetea-tabstag, not the row. Chrome shows a generic box named Sencha, around a tab list with no name. - labelledby="sencha" prints
Labelled by: Sencha. The tab list is named by the heading.
Here is how the kit does the third one:
/** The heading the labelledby attribute names, looked up in the tabs' own tree: the page, or a shadow root around them. */
#heading() {
const id = this.getAttribute("labelledby");
return id ? (this.getRootNode().getElementById?.(id) ?? null) : null;
}
#render() {
const row = this.shadowRoot.querySelector(".tabs");
const label = this.getAttribute("label");
if (label === null) row.removeAttribute("aria-label");
else row.setAttribute("aria-label", label);
const heading = this.#heading();
row.ariaLabelledByElements = heading ? [heading] : null;
row.ariaOrientation = this.getAttribute("orientation") === "vertical" ? "vertical" : null;- Lines 2 to 5 find the heading.
getRootNode()gives the tree the tabs sit in: the page, or a shadow root around them. ItsgetElementById()looks the id up there. - Line 13 hands the heading element to the row. The row is inside the shadow root, and the heading is outside. So the reference points out, and MDN says that's in scope.
- Lines 9 to 11 are lesson 10.2's
label. When both are set, the heading wins.
The heading has to be in the same tree as the tabs when they draw. The kit's JSDoc says so, as lesson 13.3 said for the quantity's label.
Leaf and Kettle's cook can see the café through the kitchen hatch. She can point at table 4 and say "that order's ready". The guests can't see into the kitchen, so they can't point at a cook.
- In real life: The kitchen
- In JavaScript: The tabs' shadow root
- In real life: The café
- In JavaScript: The page
- In real life: The cook pointing out at a table
- In JavaScript: The row's
ariaLabelledByElementspointing at the heading - In real life: A guest who can't see into the kitchen
- In JavaScript: A page element that can't point into a shadow root
The table sums it up. Read each row across:
| Where the two ends live | An id reference | An element reference | What the kit does |
|---|---|---|---|
| A tab and its panel, both in the page. | It works. Both are in one tree. | It works. | It keeps both in the page, in two slots. |
| The tab row in the shadow root, the heading in the page. | It fails. The row looks for the id in its own tree. | It works. It points out of the shadow root. | The labelledby attribute uses an element reference. |
| A tab in the page, its panel in the shadow root. | It fails, as lesson 10.1 showed. | It fails. It points into the shadow root. | It never builds panels in its shadow root. |
What about Reference Target, from lesson 13.3? It lets a host pass references on to one element inside its shadow root. Only Chrome and Edge turn it on by default, from version 152. And one element can't stand for three panels. Keeping tabs and panels in the page works in every browser.
Why it matters: a link that can't be found fails without an error. The tree is the only place you see it.
How do vertical tabs work?
A menu with many teas may list its tabs down the side. The APG has two rules for a vertical tab list:
- The tab list says so, with
aria-orientation="vertical". - Down Arrow does what Right Arrow does in a row. Up Arrow does what Left Arrow does.
The kit's tabs get an orientation attribute. Line 14 of the naming code above sets the row's ariaOrientation from it. The keydown listener now reuses keyStep(), the helper from lesson 14.2:
#onKeydown(event) {
const tabs = this.#tabs();
const from = tabs.indexOf(event.target);
const vertical = this.getAttribute("orientation") === "vertical";
const rtl = getComputedStyle(this).direction === "rtl";
const step = keyStep(event.key, { vertical, rtl });
if (from === -1 || step === null || event.altKey || event.ctrlKey || event.metaKey) return;
event.preventDefault();
const to = step === "first" ? 0 : step === "last" ? tabs.length - 1 : (from + step + tabs.length) % tabs.length;
tabs[to].focus();
if (this.getAttribute("activation") !== "manual") this.#choose(to);
}- Lines 4 and 5 read the orientation and the writing direction. Line 6 asks
keyStep()where the key goes: next, back, first, last, or nowhere. - Line 7 lets every other key pass. Keys pressed with Alt, Ctrl, or Cmd pass too, so shortcuts still work.
- Line 9 turns the step into a tab, going around the ends. Line 10 moves focus.
- Line 11 picks that tab, unless activation is manual. The next section is about that.
Why only keyStep(), and not the whole RovingFocus class? RovingFocus keeps the 0 on the item you focused last, like a bookmark. Tabs keep it on the selected tab, and #render() already does that. Try three layouts:
<div dir="ltr"> <tea-tabs label="Sencha"> <tea-tab slot="tab">Taste</tea-tab> <tea-tab slot="tab">Brewing</tea-tab> <tea-tab slot="tab">Origin</tea-tab> <tea-tab-panel slot="panel">Fresh and grassy.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel> <tea-tab-panel slot="panel">Grown in Japan.</tea-tab-panel> </tea-tabs></div><script type="module"> import "./tea-kit/index.js"; const tabs = document.querySelector("tea-tabs"); tabs.addEventListener("tea-change", (event) => console.log("Showing:", event.tab.textContent));</script>tablist "Sencha", orientation: horizontal
tab "Taste", selected: true
tab "Brewing", selected: false
tab "Origin", selected: false
tabpanel "Taste"Click Taste, then press Right Arrow, Down Arrow, and Left Arrow. Right shows Brewing, Down does nothing, and Left goes back to Taste. Down keeps its normal job of scrolling the page.
- Line 1 sets the writing direction. Line 2 is the tabs, with or without a vertical orientation.
- Line 14 prints each tab a person picks.
- Across: Right prints
Showing: Brewing. Down does nothing. Left goes back to Taste. - Vertical: only Down moves, to
Showing: Brewing. Chrome's tree saysorientation: vertical. - Right to left: Right goes back, around to
Showing: Origin. Left goes forward, around to Taste.
In a row, Up and Down do nothing, so they still scroll the page. The APG asks for exactly that. Why it matters: the keys match what people see, in any layout and any language.
Should tabs use automatic or manual activation?
Lesson 10.2 met two ways to pick a tab. With automatic activation, an arrow key picks the tab it lands on. With manual activation, the arrows only move focus. Enter or Space picks.
The APG recommends automatic activation when panels show without a noticeable delay. That usually means they're already in the page. Otherwise, its keyboard guide says, every arrow press waits for a panel. Chrome's tabs guide adds two more reasons for manual activation:
- Picking a tab has side effects, like changing the address or starting a video.
- The panels have very different heights, and push the rest of the page around.
The kit's tabs get an activation="manual" attribute. Line 11 of the keydown code then skips the pick. Enter and Space still work: a button clicks on them, and a tea-tab calls click(). Try it with two panels that load from a server:
<tea-tabs label="Sencha"> <tea-tab slot="tab">Taste</tea-tab> <tea-tab slot="tab">Brewing</tea-tab> <tea-tab slot="tab">Origin</tea-tab> <tea-tab-panel slot="panel">Fresh and grassy.</tea-tab-panel> <tea-tab-panel slot="panel">Not loaded yet.</tea-tab-panel> <tea-tab-panel slot="panel">Not loaded yet.</tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; const tabs = document.querySelector("tea-tabs"); tabs.addEventListener("tea-change", (event) => { console.log("Loading", event.tab.textContent, "from the server"); event.tab.panel.textContent = `${event.tab.textContent} is loaded.`; });</script>Click Taste, press Right Arrow twice, then Enter. Each arrow picks a tab, so the page loads Brewing, then Origin. Enter does nothing new: Origin is already picked.
- Line 1 is the only line that changes. Lines 6 and 7 are panels that haven't loaded yet.
- Lines 12 to 15 run when a tab is picked. Line 13 prints the load. Line 14 finds the panel through the tab's
panelproperty, and fills it.
- Automatic: two loads,
Loading Brewing from the server, then Origin. Enter adds nothing. - Manual: one load,
Loading Origin from the server, when you press Enter.
Where is the 0 while you arrow in manual mode? It stays on Taste, the selected tab. The APG's manual example does the same. So Tab out and Shift+Tab back lands on the tab that's showing. Exercise 2 tries it.
On a TV, you flick through channels, and each one is on at once. A film menu works differently. You move the highlight along the films, and nothing starts. When you find the one you want, you press OK, and only that film loads.
- In real life: Pressing the next-channel button
- In JavaScript: An arrow key with automatic activation
- In real life: A channel that's on at once
- In JavaScript: A panel that's already in the page
- In real life: Highlighting a film in the menu
- In JavaScript: An arrow key with manual activation
- In real life: Pressing OK to start the film
- In JavaScript: Enter or Space
- All three panels are already in the page.
- Each panel loads from the server when its tab is picked.
- Picking a tab changes the address in the address bar.
- Picking a tab starts a brewing video.
- The panels are short notes, all about the same height.
- Panels of very different heights push the rest of the page up and down.
Sort each set of tabs by the activation the APG and Chrome's tabs guide point to.
The APG lists one more optional key. If people may close tabs, Delete closes the focused tab and its panel. Focus moves to the next tab, or to the one before if it was the last. Exercise 6 builds it with the tab's panel property.
How do you test tabs with a screen reader?
Chrome's tree says what's there. A screen reader says it out loud, and each one words it differently. So what should you listen for?
The W3C's ARIA and Assistive Technologies project, ARIA-AT, tests screen readers against the APG's examples. Its test plan for tabs lists what a screen reader conveys when focus lands on a tab:
- It must say the role (tab), the name (like Brewing), and whether the tab is selected.
- It should say the tab's position and the count, like 2 of 3.
- It may say "not selected" for a tab that isn't, and the tab list's name as you enter it.
Heydon Pickering writes that a screen reader says "something similar to" the tab's label, then "selected, tab, 2 of 4". Where does the 2 of 4 come from? No attribute holds it. When every tab is in the page, WAI-ARIA says the browser works out each one's place and the count.
Testing with VoiceOver on a Mac
- Press Command-F5 to turn VoiceOver on. Press it again to turn it off.
- In the overview's result, click the heading Sencha, then press Tab. Focus lands on Taste, and VoiceOver reads it.
- Listen for the role, the name, and selected. Press Right Arrow, and listen to Brewing.
- If Right Arrow moves VoiceOver's own cursor instead, arrow-key Quick Nav is on. Press VO-Shift-Q to turn it off. VO means Control and Option together, or Caps Lock.
- Press VO-Fn-F4 to hear the focused tab again, with its state.
Testing with NVDA on Windows
- Start NVDA with Control+Alt+N. Its installer offers that shortcut.
- Open the page and press Tab until NVDA reads a tab. NVDA switches to focus mode on a control that needs it, so the arrow keys reach the tabs.
- Press Right Arrow, and listen to the next tab.
- Press NVDA+Tab to hear the focused tab again. The NVDA key is Insert, unless you changed it.
- Press NVDA+Q, then Enter, to quit.
Then try the manual choice in the activation playground. Arrow from Taste to Brewing. Brewing isn't selected now. A screen reader won't call it selected, and may say "not selected". That's how you hear manual activation working.
Asha phones Leaf and Kettle. She can't see the board, so Ravi reads it to her: "The second of three specials is masala chai. It's today's pick." She knows what it is, which one, and how many there are.
- In real life: "A special"
- In JavaScript: The role, tab
- In real life: "Masala chai"
- In JavaScript: The name
- In real life: "Today's pick"
- In JavaScript: Selected
- In real life: "The second of three"
- In JavaScript: The position and count, 2 of 3
Automated checks
Check the tree in code too. DevTools shows it in its Accessibility pane, as lesson 13.4 showed. In tests, look out for tools that work the roles out themselves. Playwright's role locators follow ARIA attributes and HTML's own roles. Here are a Node script's lines that ask Playwright:
const tabs = page.locator("tea-tabs");
console.log(await page.getByRole("tab").count());
console.log(await tabs.ariaSnapshot());Line 2 counts the tabs. Line 3 prints Playwright's own snapshot of the tree. On lesson 10.2's buttons, line 2 prints 3, and line 3 prints:
- tablist "Sencha":
- tab "Taste" [selected]
- tab "Brewing"
- tab "Origin"
- tabpanel "Taste": Fresh and grassy.On the overview's tea-tab elements, line 2 prints 0, and line 3 prints:
- tablist: Taste Brewing Origin
- text: Fresh and grassy.Playwright can't see internals, and it missed the heading's name too. Chrome's tree has all of it. So ask Chrome, through the DevTools protocol:
const cdp = await page.context().newCDPSession(page);
const { nodes } = await cdp.send("Accessibility.getFullAXTree");
for (const node of nodes.filter((each) => each.role?.value === "tab")) {
const selected = node.properties.find((property) => property.name === "selected");
console.log(node.name.value, selected.value.value);
}- Line 1 opens a DevTools protocol session for the page. Line 2 asks for Chrome's whole tree.
- Lines 3 to 6 print each tab's name and its selected state.
It prints Taste true, Brewing false, and Origin false for both pages. This course's own lesson tests read trees the same way. Why it matters: a check that can't see internals would call working tabs broken, and miss broken ones.
When should you not use tabs?
Tabs hide content until someone picks it. Heydon Pickering's Tabbed Interfaces argues that many tab sets aren't needed. A table of contents, with links to sections on the same page, often does the job. It's simpler and more robust.
The Sencha facts are three short notes. They fit on the page as they are:
<h3>Sencha</h3><h4>Taste</h4><p>Fresh and grassy.</p><h4>Brewing</h4><p>Steep for 2 minutes.</p><h4>Origin</h4><p>Grown in Japan.</p>Lines 2, 4, and 6 are headings. Nothing is hidden, and there's no script. Chrome's tree lists all four headings. In NVDA's browse mode, the H key jumps to the next one.
heading "Sencha"
heading "Taste"
heading "Brewing"
heading "Origin"- Don't use tabs for site navigation, like Home, Shop, and About. Those are links to other pages. Heydon and Chrome's tabs guide both say so.
- Don't dress an app's views up as tabs. Heydon says to use links, and move focus to the new view.
- With very many tabs, or a number you can't know ahead, Heydon suggests an accordion. Lesson 14.4 builds one.
- Use tabs when people need one panel at a time, and the tabs look and act like tabs.
Why it matters: the simplest markup that does the job is the one every reader can use.
Adding accessible tabs to the tea kit
The kit's new version is kitAfter143. Two files are new, and one changes:
tea-kit/tea-tab.jsistea-tab: its role, state, and panel live in its internals.tea-kit/tea-tab-panel.jsistea-tab-panel: its role and its name live in its internals.tea-kit/tea-tabs.jsgetslabelledby,orientation, andactivation, keys fromkeyStep(), and two ways to draw.
With buttons and no new attributes, the tabs work as before. The lesson's test runs lesson 10.2's and lesson 13.2's own pages on both versions to prove it. Here the kit makes a brewing guide:
<link rel="stylesheet" href="tea-kit/tokens.css"><h3 id="guide">Brewing guide</h3><tea-tabs labelledby="guide" orientation="vertical"> <tea-tab slot="tab">Green</tea-tab> <tea-tab slot="tab">Black</tea-tab> <tea-tab slot="tab">Oolong</tea-tab> <tea-tab-panel slot="panel"><tea-card name="Sencha" price="4"></tea-card></tea-tab-panel> <tea-tab-panel slot="panel"><tea-card name="Assam" price="3"></tea-card></tea-tab-panel> <tea-tab-panel slot="panel"><tea-card name="Tieguanyin" price="6"></tea-card></tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; const tabs = document.querySelector("tea-tabs"); tabs.addEventListener("tea-change", (event) => console.log("Showing:", event.tab.textContent));</script>- Line 3 names the tabs after the heading on line 2, and stacks them in a column.
- Lines 7 to 9 put a kit tea card in each panel.
Click Green, then press Down Arrow: Showing: Black. Press Tab: focus goes to the Black panel. Press Tab again: the Assam card's - button. Chrome's tree:
heading "Brewing guide"
tablist "Brewing guide", orientation: vertical
tab "Green", selected: false
tab "Black", selected: true
tab "Oolong", selected: false
tabpanel "Black"
heading "Assam"The JSDoc and custom-elements.json document both new tags and all three new attributes. For the keyboard events behind the keys, see Keyboard and focus on the JavaScript path.
Common mix-ups
- "A component should write role attributes on its own tag." No. Internals give default roles and states. Attributes are the page's to write, and they win.
- "aria-labelledby on the tabs' tag names the tab list." No. It names the tag. The tab list inside the shadow root stays unnamed.
- "An element reference can point into a shadow root." No. It can only point out. In needs Reference Target, and only Chrome and Edge have it by default.
- "Tabs should always pick a tab as soon as it gets focus." No. When panels are slow, or picking has side effects, use manual activation.
- "In manual mode, Tab comes back to the last tab you arrowed to." No. The 0 stays on the selected tab, so Tab comes back to it.
- "Vertical tabs also move with Left and Right." No. Up and Down move. Left and Right keep their normal job.
- "If Playwright finds no tabs, a screen reader won't either." No. Playwright works roles out from attributes. Chrome's tree, which screen readers read, has the roles from internals.
- "Any set of sections should be tabs." No. Headings on one page are often simpler, and work for everyone.
Practice exercises
For each one, ask which tab is selected, where focus is, and which tree each end lives in.
Click Taste, then press Down Arrow twice. Which tab does the second press pick?
<div dir="ltr"> <tea-tabs label="Sencha" orientation="vertical"> <tea-tab slot="tab">Taste</tea-tab> <tea-tab slot="tab">Brewing</tea-tab> <tea-tab slot="tab">Origin</tea-tab> <tea-tab-panel slot="panel">Fresh and grassy.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel> <tea-tab-panel slot="panel">Grown in Japan.</tea-tab-panel> </tea-tabs></div><script type="module"> import "./tea-kit/index.js"; const tabs = document.querySelector("tea-tabs"); tabs.addEventListener("tea-change", (event) => console.log("Showing:", event.tab.textContent));</script>Origin. In a vertical tab list, Down Arrow does what Right Arrow does in a row. Taste to Brewing, then Brewing to Origin, and each one is picked.
Click Taste. Press Right Arrow twice, then Tab, then Shift+Tab. Where does Shift+Tab put focus?
<tea-tabs label="Sencha" activation="manual"> <tea-tab slot="tab">Taste</tea-tab> <tea-tab slot="tab">Brewing</tea-tab> <tea-tab slot="tab">Origin</tea-tab> <tea-tab-panel slot="panel">Fresh and grassy.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel> <tea-tab-panel slot="panel">Grown in Japan.</tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; document.addEventListener("focusin", (event) => console.log("Focus:", event.target.textContent));</script>Taste. The page prints Focus: Taste, Brewing, Origin, then the panel, then Taste. Manual activation never moved the selection, so Taste kept tabindex 0. In Chrome 156 and older, Tab from Origin went back to Taste instead, because those versions wrapped Tab around inside a slot.
The tab list is named by a copy of the heading's text. Name it by the heading itself. What does the page print then?
<h3 id="guide">Brewing guide</h3><tea-tabs label="Brewing guide"> <tea-tab slot="tab">Green</tea-tab> <tea-tab slot="tab">Black</tea-tab> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 4 minutes.</tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; const row = document.querySelector("tea-tabs").shadowRoot.querySelector('[role="tablist"]'); console.log("Labelled by:", row.ariaLabelledByElements?.[0]?.localName ?? "nothing");</script><h3 id="guide">Brewing guide</h3><tea-tabs labelledby="guide"> <tea-tab slot="tab">Green</tea-tab> <tea-tab slot="tab">Black</tea-tab> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 4 minutes.</tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; const row = document.querySelector("tea-tabs").shadowRoot.querySelector('[role="tablist"]'); console.log("Labelled by:", row.ariaLabelledByElements?.[0]?.localName ?? "nothing");</script>Labelled by: h3. Only line 2 changes. The tabs look up guide in the page and hand the heading to the row's ariaLabelledByElements. The name is the same, but now it follows the heading.
A teammate's tab turns bold when clicked, but a screen reader never hears "selected". Which internals property does the click forget to set?
<style>info-tab { padding: 4px 8px; } info-tab:state(selected) { font-weight: bold; }</style><div role="tablist" aria-label="Sencha"> <info-tab>Taste</info-tab> <info-tab>Brewing</info-tab></div><script> class InfoTab extends HTMLElement { #internals = this.attachInternals(); constructor() { super(); this.#internals.role = "tab"; this.addEventListener("click", () => { for (const tab of this.parentElement.children) { tab.#internals.states.delete("selected"); } this.#internals.states.add("selected"); console.log("Picked:", this.textContent); }); } } customElements.define("info-tab", InfoTab);</script><style>info-tab { padding: 4px 8px; } info-tab:state(selected) { font-weight: bold; }</style><div role="tablist" aria-label="Sencha"> <info-tab>Taste</info-tab> <info-tab>Brewing</info-tab></div><script> class InfoTab extends HTMLElement { #internals = this.attachInternals(); constructor() { super(); this.#internals.role = "tab"; this.addEventListener("click", () => { for (const tab of this.parentElement.children) { tab.#internals.ariaSelected = "false"; tab.#internals.states.delete("selected"); } this.#internals.ariaSelected = "true"; this.#internals.states.add("selected"); console.log("Picked:", this.textContent); }); } } customElements.define("info-tab", InfoTab);</script>ariaSelected. Line 13 now sets ariaSelected = "false" on every tab, and a new line 14 sets it to "true" on the clicked one. The custom state only styles the tab. Chrome's tree showed Brewing selected: false before the fix, and selected: true after.
The Origin panel loads a map from the server. Asha arrows past it to get back to Taste. Fix the tabs so passing doesn't load it. How many times does Right Arrow, Right Arrow, Right Arrow print Loading the map now?
<tea-tabs label="Sencha"> <tea-tab slot="tab">Taste</tea-tab> <tea-tab slot="tab">Brewing</tea-tab> <tea-tab slot="tab">Origin</tea-tab> <tea-tab-panel slot="panel">Fresh and grassy.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel> <tea-tab-panel slot="panel">A map of the farm goes here.</tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; document.querySelector("tea-tabs").addEventListener("tea-change", (event) => { if (event.tab.textContent === "Origin") console.log("Loading the map"); });</script><tea-tabs label="Sencha" activation="manual"> <tea-tab slot="tab">Taste</tea-tab> <tea-tab slot="tab">Brewing</tea-tab> <tea-tab slot="tab">Origin</tea-tab> <tea-tab-panel slot="panel">Fresh and grassy.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel> <tea-tab-panel slot="panel">A map of the farm goes here.</tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; document.querySelector("tea-tabs").addEventListener("tea-change", (event) => { if (event.tab.textContent === "Origin") console.log("Loading the map"); });</script>0 times. Only line 1 changes. With manual activation the arrows only move focus, so passing Origin picks nothing. The map loads when someone presses Enter or Space on Origin, or clicks it.
Let people close a tab with Delete, as the APG allows. Click Brewing and press Delete. Which tab gets focus?
<tea-tabs label="Open notes"> <tea-tab slot="tab">Taste</tea-tab> <tea-tab slot="tab">Brewing</tea-tab> <tea-tab slot="tab">Origin</tea-tab> <tea-tab-panel slot="panel">Fresh and grassy.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel> <tea-tab-panel slot="panel">Grown in Japan.</tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; const tabs = document.querySelector("tea-tabs"); document.addEventListener("focusin", (event) => console.log("Focus:", event.target.textContent)); // Close the focused tab, and its panel, with Delete.</script><tea-tabs label="Open notes"> <tea-tab slot="tab">Taste</tea-tab> <tea-tab slot="tab">Brewing</tea-tab> <tea-tab slot="tab">Origin</tea-tab> <tea-tab-panel slot="panel">Fresh and grassy.</tea-tab-panel> <tea-tab-panel slot="panel">Steep for 2 minutes.</tea-tab-panel> <tea-tab-panel slot="panel">Grown in Japan.</tea-tab-panel></tea-tabs><script type="module"> import "./tea-kit/index.js"; const tabs = document.querySelector("tea-tabs"); document.addEventListener("focusin", (event) => console.log("Focus:", event.target.textContent)); tabs.addEventListener("keydown", (event) => { if (event.key !== "Delete" || event.target.localName !== "tea-tab") return; const all = [...tabs.querySelectorAll("tea-tab")]; const index = all.indexOf(event.target); const next = all[index + 1] ?? all[index - 1]; event.target.panel.remove(); event.target.remove(); next?.focus(); });</script>Origin. Lines 13 to 21 replace the comment. They find the tab after the focused one, or the one before, remove the tab and its panel, and focus the next tab. The kit's slotchange draws again, and Origin, now in Brewing's place, is selected.
Check your understanding
For each question, ask which tree holds the answer: the markup, or the accessibility tree.
Question 1 of 8In the kit's tabs, which element has the role tablist?
Choose an answer to see the explanation.
Question 2 of 8Taste is selected. The page writes
aria-selected="true"on the Brewingtea-tab. What does Chrome's tree show for Brewing?Choose an answer to see the explanation.
Question 3 of 8The tab list is in the shadow root, and the heading Sencha is on the page. What names the tab list from the heading?
Choose an answer to see the explanation.
Question 4 of 8A tab list has
aria-orientation="vertical". Which keys move between its tabs?Choose an answer to see the explanation.
Question 5 of 8When does the APG recommend automatic activation?
Choose an answer to see the explanation.
Question 6 of 8Manual activation. Taste is selected, and two presses of Right Arrow moved focus to Origin. Which tab has
tabindex="0"?Choose an answer to see the explanation.
Question 7 of 8Focus lands on a selected tab. What must a screen reader say, by ARIA-AT's test plan?
Choose an answer to see the explanation.
Question 8 of 8What does
await page.getByRole("tab").count()give in Playwright for the overview'stea-tabelements?Choose an answer to see the explanation.
Key takeaways
- The ARIA tabs pattern is a tab list of tabs, one selected, each controlling a panel it names.
- Audit tabs against the APG in Chrome's accessibility tree, role by role and state by state.
- Give your own elements their roles and states from ElementInternals. Attributes are the page's to write.
- Keep tabs and panels in one tree. Point out of a shadow root with element references, never in.
- Name the tab list by its visible heading, with an element reference from inside the shadow root.
- Rows use Left and Right, swapped right to left. Columns use Up and Down and say
aria-orientation="vertical". - Use automatic activation when panels show at once, and manual activation when they don't.
- Keep the 0 on the selected tab, so Tab always comes back to it.
- A screen reader must say the role, the name, and selected. It should say 2 of 3.
- Test with Chrome's own tree. Tools that work roles out themselves can miss internals.
Remember the one-liner.
One tab list, one selected tab, each tab naming its panel, and keys that follow the layout.
Sources
The facts on this page come from these standards, docs, and posts:
- Tabs pattern, W3C WAI-ARIA Authoring Practices.
- WAI-ARIA 1.2: the tablist role, W3C.
- Tabbed interfaces, Chrome for Developers, Modern Web Guidance.
- HTML Standard: custom elements, accessibility semantics, WHATWG.
- ElementInternals: properties included from ARIA, MDN.
- Accessible web components, Chrome for Developers, Modern Web Guidance.
- HowTo: Components, howto-tabs, Google Chrome Labs.
- Reflected attributes: reflected element reference scope, MDN.
- Example of Tabs with Manual Activation, W3C WAI-ARIA Authoring Practices.
- Developing a Keyboard Interface: deciding when to make selection automatically follow focus, W3C WAI-ARIA Authoring Practices.
- ARIA-AT test plan for tabs with manual activation: assertions, W3C ARIA and Assistive Technologies project.
- WAI-ARIA 1.2: aria-posinset, W3C.
- Get started with VoiceOver on Mac, Apple.
- VoiceOver orientation commands on Mac, Apple.
- Use VoiceOver Quick Nav in apps and webpages on Mac, Apple.
- NVDA User Guide, NV Access.
- Locators: locate by role, Playwright.
- Accessibility domain: getFullAXTree, Chrome DevTools Protocol.
- Tabbed Interfaces, Heydon Pickering, Inclusive Components.
- browser-compat-data, MDN.
Coming next: An accessible accordion, in Accessible components. It builds an accordion with headings, buttons, and states that announce correctly.