Home / Website Sections / Tabs and Accordion Kit

Tabs and Accordion Kit in JavaScript, Free with Live Demo

Free accessible tabs and accordion in plain JavaScript. Correct ARIA roles, arrow key navigation, one or many panels open, smooth height animation and deep links.

Open live demoDownload HTML fileView code on GitHub
Tabs and Accordion Kit JavaScript project: accessible tabs and accordions with keyboard support and smooth height animation

Runs on: ARIA roles and the Web Animations API. Works in all modern browsers.

What is the Tabs and Accordion Kit?

A tabs component and an accordion that follow the official accessibility patterns. They work with a mouse, a keyboard and screen readers.

Tabs support arrow keys, Home and End, and remember the selected tab in the address bar. The accordion can allow one or many open items and animates smoothly.

Good for

  • Product pages with specs and reviews
  • FAQ sections
  • Pricing and plan comparisons
  • Settings pages

What this project does

How it works

  1. Roving tabindexOnly the selected tab is in the Tab order. Arrow keys move selection, so one Tab press takes you into the panel.
  2. Link tabs and panelsaria-controls and aria-labelledby connect each tab to its panel, so screen readers announce them together.
  3. Animate real heightThe accordion measures scrollHeight and animates from 0 to that height with element.animate().

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

tabs.forEach((tab, i) => tab.onkeydown = (e) => {
  const next = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: tabs.length - 1 }[e.key];
  if (next === undefined) return;
  select(tabs[(next + tabs.length) % tabs.length]);
});
function select(tab) {
  tabs.forEach((t) => {
    const on = t === tab;
    t.setAttribute("aria-selected", on);
    t.tabIndex = on ? 0 : -1;
    document.getElementById(t.getAttribute("aria-controls")).hidden = !on;
  });
  tab.focus();
}

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

Why not use details and summary for the accordion?

You can for simple cases. This version adds a one-at-a-time mode and smooth animation, which details cannot do on its own.

Should tab panels be hidden or removed?

Hidden. The content stays in the page, which is better for search engines and for find in page.

Do tabs change the URL?

Yes, the hash updates, so you can link straight to the Reviews tab.

More Website Sections projects