Skip to main content
Login

Sign in to access your account

Accordion - National Design System

A stack of headings that each open a panel of content in place

Overview

An accordion is a stack of items. Each item has a heading with a button, and a panel under it that the button opens and closes. The panels open in place and push the content below them down.

Pick another component when:

  • people switch between views in one space, and see one at a time: Tabs
  • people must compare the content side by side: Cards
  • the panel opens over the page, not in it: Dropmenu

Markup

Preview

An overview of the service and how to begin your application.

The documents and the eligibility criteria you need before you apply.

The fee schedule and the expected processing time for each request.

<div class="nds-accordion" id="service-faq"> <div class="nds-accordion-item"> <h3 class="nds-accordion-header" id="faq-heading-1"> <button type="button" class="nds-btn nds-subtle nds-menu-btn nds-accordion-btn" aria-expanded="true" data-state="open" aria-controls="faq-panel-1"> <span class="nds-accordion-title">Getting started</span> </button> </h3> <div class="nds-accordion-collapse" id="faq-panel-1" data-state="open"> <div class="nds-accordion-content"> <div class="nds-accordion-body"> <p>An overview of the service and how to begin your application.</p> </div> </div> </div> </div> <div class="nds-accordion-item"> <h3 class="nds-accordion-header" id="faq-heading-2"> <button type="button" class="nds-btn nds-subtle nds-menu-btn nds-accordion-btn" aria-expanded="false" aria-controls="faq-panel-2"> <span class="nds-accordion-title">Requirements</span> </button> </h3> <div class="nds-accordion-collapse" id="faq-panel-2"> <div class="nds-accordion-content"> <div class="nds-accordion-body"> <p>The documents and the eligibility criteria you need before you apply.</p> </div> </div> </div> </div> <div class="nds-accordion-item"> <h3 class="nds-accordion-header" id="faq-heading-3"> <button type="button" class="nds-btn nds-subtle nds-menu-btn nds-accordion-btn" aria-expanded="false" aria-controls="faq-panel-3"> <span class="nds-accordion-title">Fees and processing time</span> </button> </h3> <div class="nds-accordion-collapse" id="faq-panel-3"> <div class="nds-accordion-content"> <div class="nds-accordion-body"> <p>The fee schedule and the expected processing time for each request.</p> </div> </div> </div> </div> </div>

Behavior

One Open Item

By default, one item is open at a time. When a person opens an item, the open one closes, so the list stays short. Pick it for questions and answers, where people read one answer and move on.

When a long item above closes, the page moves up. If the heading of the item that opened is then above the screen, the page scrolls back to it.

Always Open

data-state="always-open" on .nds-accordion lets each item open and close on its own. Items stay open until the person closes them. Pick it when people compare two answers, or keep one panel open while they work in another.

Built-in Features

  • Auto-initialization

    Every accordion on the page starts by itself. Opening, closing and the keyboard need no call.

  • Animations

    Panels slide open and closed. The motion stops when the user prefers reduced motion, and an item that is open at load does not animate.

  • Keyboard Navigation

    The up and down arrows move between the headings and wrap at the ends. Home and End jump to the first and the last heading. Enter or Space opens or closes the focused item.

  • Loading Skeleton

    Gray bars stand in for the titles and the open panel until the script starts. In a list that a filter controls, they stay until the filter has applied the choices in the URL.

  • Nested Accordions

    A panel can hold another accordion. Each accordion runs its own items and ignores the items of the one inside it.

  • Print

    Every panel prints open, so no content is lost on paper.

  • Screen Readers and High Contrast

    The script keeps aria-expanded on each button in step with its panel. In high-contrast mode, the lines between items get stronger and the focus ring gets wider.

  • Open and Close Events

    An event fires when a panel has finished opening or closing, for analytics or for content that loads on open.

Best Practices

  • Use an accordion when people scan the headings and open only what they need: questions and answers, services grouped by topic, settings.
  • Keep titles short. People decide to open an item from its title alone.
  • Put the item people open most first, and open it at load when most visitors need it.
  • Pick the heading level that fits the page outline. The markup above uses h3, under an h2 section title.
  • Keep one button and one nds-accordion-collapse in each item. The script pairs them by their order in the markup.
  • Give each button aria-controls with the id of its collapse.
  • Do not add role="region", aria-label or aria-labelledby to nds-accordion-collapse. On a long list, every panel becomes a landmark. On a plain div, the two labels are not allowed, so an accessibility audit fails.
  • Use nds-minimal for one or two items inside other content, such as the requirements of a step. On a page section, keep the full-width headings: they are easier to find and to tap.
  • Do not hide content that people must see to finish a task, such as a required step or a warning.
  • After you add items to a live accordion, call NDS.Accordion.init(). The new items start working, and the open items stay open.

API

Data Attributes

Attribute Element Effect
aria-expanded="true" and data-state="open" .nds-accordion-btn Opens the item at load. The script reads aria-expanded. data-state turns the arrow before the script runs
data-state="open" .nds-accordion-collapse Shows the panel at load, before the script runs. Set it with the two attributes above
data-state="always-open" .nds-accordion Several items can stay open at once. Without it, opening one item closes the others. Set it yourself
data-state="loading" .nds-accordion The same as nds-loading, for a script that already sets states

CSS Custom Properties

Set them on .nds-accordion, in its style attribute.

Property Default Controls
--accordion-header-height 56px, 48px with nds-md, 40px with nds-sm Minimum height of each heading button. Not used with nds-minimal
--accordion-header-padding var(--spacing-xl), var(--spacing-lg) with nds-md, var(--spacing-md) with nds-sm Padding above and below the title in each heading button. With nds-minimal, padding above and below each panel instead
--accordion-scroll-offset 120px Space between the main navigation and an opened item’s heading, when the page scrolls back to it

Tokens

It reads these shared tokens. A change to one also changes other components: see Tokens.

Source: the var() reads in _sass/components/_accordion.scss.

TokenPreviewValue
--text-default
Read by color

var(--colors-base-black)
Dark mode var(--colors-neutral-200)
--text-primary
Read by border-color

var(--colors-primary-600)
Dark mode var(--colors-primary-400)
--divider-color
Read by border-top

var(--colors-alpha-black-10)
Dark mode var(--colors-alpha-white-10)
--button-background-secondary-hovered
Read by --btn-bg-hover

var(--colors-neutral-100)
Dark mode var(--colors-neutral-700)
--button-background-secondary-pressed
Read by --btn-bg-active

var(--colors-neutral-200)
Dark mode var(--colors-neutral-600)
--button-background-transparent-default
Read by --btn-bg, --btn-bg-selected
var(--colors-alpha-white-0)
--link-neutral
Read by --btn-color

var(--colors-neutral-700)
Dark mode var(--colors-neutral-200)
--link-neutral-hovered
Read by --btn-color-hover

var(--colors-neutral-500)
Dark mode var(--colors-neutral-400)
--link-neutral-pressed
Read by color

var(--colors-neutral-400)
Dark mode var(--colors-neutral-500)

JavaScript

The NDS.Accordion script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.

The instance lives on .nds-accordion as ndsAccordion. It is there once the component starts: code that runs at page load waits for NDS.init.ready. Item positions start at 0.

Method Effect
NDS.Accordion.init() Starts every accordion on the page that has not started yet. On an accordion that already started, it picks up the new items
NDS.Accordion.create(element) Starts one accordion and returns its instance, or the instance it already has. Returns null when it has no buttons
NDS.Accordion.destroy(container) The same as instance.destroy(). The open items stay open
instance.openItem(index), instance.closeItem(index), instance.toggleItem(index) Open, close or toggle an item by its position
instance.openAll() Opens every item, also in an accordion without always-open
instance.closeAll() Closes every open item
instance.getOpenItems() Returns the open items, each as { index, button, collapse, isOpen }
instance.refresh() Picks up the items put into this accordion after it started
instance.destroy() Removes the listeners. The markup stays as it is, and the open items stay open
Event Fired on Detail
nds:accordion:opened .nds-accordion, after the panel opens, and it bubbles index, button, collapse, accordion
nds:accordion:closed .nds-accordion, after the panel closes, and it bubbles index, button, collapse, accordion
var faq = document.querySelector('#service-faq'); faq.addEventListener('nds:accordion:opened', function (e) { console.log('Opened item', e.detail.index); }); faq.ndsAccordion.openItem(2);

The full API is in the banner of _js/nds-accordion.js.

Last Modified Date: 05/11/2026 - 12:02 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks