Skip to content

Tabs ​

Tabs fail more frequently than almost any other bespoke widget on the web. While pointer click toggling is trivial to construct, roving tabindex keyboard navigation and bidirectional WAI-ARIA synchronization are frequently omitted entirely, leading to broken assistive technology contracts.

Live Demo ​

Tab into the tablist below, then explore navigating using the Left and Right arrow keys (← / →):

Visual Variant (Filled) ​

Visual selection treatments—whether utilizing sliding underlines or filled background pills—execute the identical initTabs() script logic. Only CSS presentation styles differ.

Keyboard Interaction ​

Keyboard operations and expected behavior
Key InteractionExpected Behavioral Contract
TabEnter the tablist in a single stop; exit to the next interactive page element in a single stop
Shift + TabExit the tablist backward to the previous interactive page element in a single stop
← / → (Horizontal)Move keyboard focus and active selection to the previous / next tab
Home / EndJump focus directly to the first / last tab in the list

Pointer Interactions ​

Pointer operations and expected behavior
Pointer ActionExpected Behavioral Contract
Click / Tap TabSelects the target tab and displays the corresponding panel (producing identical results to keyboard activation)

Structure ​

Profilearia-selected="true"tabindex="0"Billingaria-selected="false"tabindex="-1"Left / Right arrow: move focus + selection togetherrole="tabpanel" for the selected tab; the rest stay hidden

Rules ​

Give tabs and panels their roles and link them both ways MUSTUI-R15
Use role="tablist", role="tab" and role="tabpanel"; each tab's aria-controls points to its panel and each panel's aria-labelledby points to its tab. Screen readers announce the number of tabs, the selected tab and the panel's name.
Check: TAB-01TAB-01 Tab and panel linking✕ NGTab and panel are not linkedThe tab has no aria-controls and the panel has no aria-labelledby, so they are not linked✓ OKTab and panel point to each otherAria-controls on the tab and aria-labelledby on the panel point to each other
Put only the selected tab in the Tab order MUSTUI-R16
The selected tab has tabindex="0" and the others tabindex="-1" (roving tabindex). One Tab press enters the tab list and the next one leaves it.
Check: TAB-02TAB-02 Tab key in the tab list✕ NGTakes 3 Tab presses to get outAll three tabs have tabindex 0, so it takes three Tab presses to leave the tab list✓ OKOne Tab in, the next Tab outOnly the selected tab has tabindex 0, so one Tab press leaves the tab list for the panel
Move between tabs with the arrow keys and Home / End MUSTUI-R17
← → (↑ ↓ for vertical tabs) move focus and selection to the previous or next tab, and Home End to the first or last. Even with many tabs, few key presses reach the one users want.
Check: TAB-03TAB-03 Arrow keys on tabs✕ NGFocus moves but the panel staysThe arrow key moves only focus to tab C, and panel B stays shown✓ OKFocus and panel switch togetherThe arrow key moves focus and the shown panel together to tab C
Hide unselected panels with the hidden attribute MUSTUI-R18
A panel hidden only with a CSS class can still be read out. The hidden attribute removes it from both the screen and screen readers.
Check: TAB-04TAB-04 Hiding unselected panels✕ NGHidden panel is still readAn unselected panel hidden only by a CSS class is still read aloud✓ OKhidden also hides it from readersAn unselected panel with the hidden attribute is not read
Keep the focus indicator visible on the selected tab MUSTUI-R19
When focus lands on a tab that is filled or underlined to show selection, the indicator must still stand out from its background. The focus position does not get lost in the selection style.
Check: TAB-05TAB-05 Focus on the selected tab✕ NGFocus is invisible when selectedThe selected tab is filled blue, so the blue focus ring blends into it and is invisible✓ OKFocus stays visible when selectedEven on the selected tab, the focus ring sits outside with a gap and stays visible
Do not shift the tabs when the selection changes SHOULDUI-R20
Bolding only the selected tab changes its width and moves every tab after it. Keep the weight the same, or absorb the width change with padding.
Check: TAB-06TAB-06 Tabs do not shift✕ NGFollowing tabs shift on selectOnly the selected tab is bold and wider, so the following tab shifts between states✓ OKPositions do not moveThe font weight stays the same when selected, so the following tab does not move

Implementation (Selection Follows Focus) ​

A complete, production-grade reference satisfying all criteria above:

View Source Code (HTML Markup and JavaScript Module)
html
<div class="tabs-wrap">
  <div role="tablist" aria-label="Account settings">
    <button role="tab" id="tab-profile" aria-selected="true" aria-controls="panel-profile">
      Profile
    </button>
    <button role="tab" id="tab-billing" aria-selected="false" aria-controls="panel-billing" tabindex="-1">
      Billing
    </button>
  </div>
  <span class="tab-indicator" aria-hidden="true"></span>
</div>
<div role="tabpanel" id="panel-profile" aria-labelledby="tab-profile">…</div>
<div role="tabpanel" id="panel-billing" aria-labelledby="tab-billing" hidden>…</div>
javascript
// modules/tabs.js
export function initTabs(root) {
  const tabs = [...root.querySelectorAll('[role="tab"]')]
  const panels = tabs.map((tab) => document.getElementById(tab.getAttribute('aria-controls')))
  const indicator = root.querySelector('.tab-indicator')

  function moveIndicator(tab) {
    if (!indicator || !tab) return
    indicator.style.left = `${tab.offsetLeft}px`
    indicator.style.width = `${tab.offsetWidth}px`
  }

  function select(index) {
    tabs.forEach((tab, i) => {
      const active = i === index
      tab.setAttribute('aria-selected', String(active))
      tab.tabIndex = active ? 0 : -1
      if (panels[i]) {
        panels[i].hidden = !active
      }
    })
    tabs[index].focus()
    moveIndicator(tabs[index])
  }

  tabs.forEach((tab, index) => {
    tab.addEventListener('click', () => select(index))
    tab.addEventListener('keydown', (event) => {
      const last = tabs.length - 1
      if (event.key === 'ArrowRight') {
        select(index === last ? 0 : index + 1)
      } else if (event.key === 'ArrowLeft') {
        select(index === 0 ? last : index - 1)
      } else if (event.key === 'Home') {
        select(0)
      } else if (event.key === 'End') {
        select(last)
      } else {
        return
      }
      event.preventDefault()
    })
  })

  const activeTab = tabs.find((tab) => tab.getAttribute('aria-selected') === 'true')
  if (activeTab) {
    moveIndicator(activeTab)
  }
}