Skip to main content
Login

Sign in to access your account

Tabs - National Design System

Group related content into a single surface where users switch between views without leaving the page

Overview

Tabs show one panel of related content at a time. A tab set is nds-tabs: a list of nds-tab buttons and an nds-tab-content area with one nds-tab-panel for each tab. Tab one opens panel one, tab two opens panel two, and so on, by their order in the markup.

Pick another component when:

  • the user must finish steps in order: Stepper
  • a few short views show the same data, such as a date range or a status filter: Content Switcher
  • the sections can all be open at once, one under another: Accordion

Markup

Options
Structure
Layout
Size
Card
Divided
More
Preview

Apply for the service online in three steps.

<div class="nds-tabs"> <div class="nds-tab-list-container nds-scroll-more"> <nav class="nds-tab-list nds-scroll-more-content" role="tablist" aria-label="Service details"> <button type="button" class="nds-btn nds-subtle nds-tab" role="tab" aria-selected="true" aria-controls="panel-overview" id="tab-overview" tabindex="0"> <span class="nds-label">Overview</span> </button> <button type="button" class="nds-btn nds-subtle nds-tab" role="tab" aria-selected="false" aria-controls="panel-requirements" id="tab-requirements" tabindex="-1"> <span class="nds-label">Requirements</span> </button> <button type="button" class="nds-btn nds-subtle nds-tab" role="tab" aria-selected="false" aria-controls="panel-fees" id="tab-fees" tabindex="-1"> <span class="nds-label">Fees</span> </button> <button type="button" class="nds-btn nds-subtle nds-tab" role="tab" aria-selected="false" aria-controls="panel-contact" id="tab-contact" tabindex="-1"> <span class="nds-label">Contact</span> </button> </nav> <button type="button" class="nds-btn nds-subtle nds-tab nds-show-more" aria-label="Show more" data-i18n-aria-label="scroll-more.show_more"> <i class="nds-icon nds-hgi-arrow-down-01" aria-hidden="true"></i> </button> </div> <div class="nds-tab-content"> <div class="nds-tab-panel" role="tabpanel" id="panel-overview" aria-labelledby="tab-overview" tabindex="0"> <p>Apply for the service online in three steps.</p> </div> <div class="nds-tab-panel" role="tabpanel" id="panel-requirements" aria-labelledby="tab-requirements" tabindex="-1" hidden> <p>A valid national ID and a recent photo.</p> </div> <div class="nds-tab-panel" role="tabpanel" id="panel-fees" aria-labelledby="tab-fees" tabindex="-1" hidden> <p>The service fee is 100 riyals, paid online.</p> </div> <div class="nds-tab-panel" role="tabpanel" id="panel-contact" aria-labelledby="tab-contact" tabindex="-1" hidden> <p>Call 19911 from Sunday to Thursday.</p> </div> </div> </div>

Built-in Features

  • Auto-initialization

    Every .nds-tabs on the page starts by itself. Tab changes, the keys and the scroll need no call.

  • Full Keyboard Navigation

    The arrow keys move between tabs, and left and right follow the reading direction. Home and End jump to the first and the last tab. Enter or Space opens the focused tab.

  • Overflow Scrolling

    A tab row that does not fit scrolls, with a fade at its edges. It takes a drag, the mouse wheel and a show-more button.

  • Vertical Layout

    nds-vertical stacks the tabs beside the panels, and the list stays in view while the page scrolls.

  • Loading Skeleton

    Gray bars stand in for the labels and the open panel until the script starts, so the tab row is never bare. The panels marked hidden stay hidden.

  • High Contrast and Reduced Motion

    The open tab gets a border in high-contrast mode, and the transitions stop when the user prefers reduced motion.

  • Tab Change Events

    Every switch fires nds:tabs:change with the new and the previous tab and panel.

  • Programmatic Control

    Open a tab, read the open panel or remove the behavior through NDS.Tabs or the element's ndsTabs property.

Best Practices

  • Use tabs for related content that people read one part at a time, such as the details of a service, or to compare options without leaving the page.
  • Leave content that fits on one page as one page. Tabs add a click to reach each part.
  • Keep labels to one to three words. Long labels make the row scroll sooner.
  • Aim for three to seven tabs. The row scrolls past that, but people may miss the hidden tabs.
  • Put the tab people open most first. It opens when the page loads.
  • Add icons only when the tabs name distinct categories, and leave them off long labels. Give each icon aria-hidden="true".
  • Pick Vertical when the labels must show at every width. Pick Rail when icons people know can stand in for the labels on a phone.
  • To show a count on a tab with an icon, such as new messages, call NDS.setBadge(tab, count). It draws the count on the icon and adds it to the tab’s name (“Inbox (2)”).
  • Keep the tabs and the panels in the same order and the same number: tab one opens panel one.
  • To keep the open tab in the address, as a hash route does, set location.hash on nds:tabs:change, and on hashchange open the matching tab with el.ndsTabs.goTo(index). Each switch is then a history entry, so Back opens the tab before it. history.replaceState drops the entry, and Back leaves the page.
  • Give the tab list an aria-label, and pair each tab and panel with aria-controls and aria-labelledby.
  • Build a long panel from nds-block parts with nds-block-title headings, so it reads in sections.
  • When one panel cannot show its content (not found, a failed load), put an Alert or Empty in it, never a Status Section. The other tabs stay open to the user.
  • When the tabs sit in a box with its own padding, such as a Panel, a Modal or a card, add nds-flush-inline to each panel. The content then lines up with the rest of the box, not inset again.
  • When a panel holds content with its own edge, such as a grid of cards, add a flush helper to each panel. nds-flush-inline lines the content up with the tab row, nds-flush-block removes the space above and below, and nds-flush removes both. See Helper Classes.

API

Data Attributes

Attribute Element Effect
aria-selected="true" .nds-tab Marks the tab that opens when the page loads. With none, the first tab opens
data-state="loading" .nds-tabs or .nds-tab-list The same as nds-loading, for a script that already sets states

CSS Custom Properties

Property Default Controls
--btn-indicator-size 3px Thickness of the open tab’s bar and of the divided line
--tab-button-gap var(--spacing-xs) Gap between a tab’s icon and its label
--tab-button-padding-block 0 Padding above and below a tab’s label
--tab-button-padding-inline the button padding Padding at the start and the end of a tab: 12px with nds-sm, 16px at MD, 20px with nds-lg. A Rail on a phone makes each tab square: 8px at MD
--tab-panel-padding var(--spacing-2xl) Padding of a panel on both axes. A card sets 0, because the card holds the padding. Vertical tabs set 8px at the top, so the first line meets the first tab. A panel-only card keeps its own padding
--tab-panel-padding-inline, --tab-panel-padding-block --tab-panel-padding Padding of a panel on one axis. A Rail on a phone sets 8px at the sides
--scroll-fade 48px Width of the fade at the edges of a tab row that scrolls. Set it on .nds-scroll-more

Tokens

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

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

TokenPreviewValue
--text-default
Read by color

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

var(--colors-neutral-700)
Dark mode var(--colors-neutral-300)
--text-default-disabled
Read by color
var(--colors-neutral-400)
--border-primary
Read by border, border-bottom
var(--colors-primary-600)
--divider-color
Read by background-color, --scroll-divider

var(--colors-alpha-black-10)
Dark mode var(--colors-alpha-white-10)
--button-indicator-mark
Read by background-color

var(--background-primary)
Dark mode var(--colors-primary-500)

JavaScript

The NDS.Tabs 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 the element as ndsTabs. It is there once the component starts: code that runs at page load waits for NDS.init.ready. The same controller runs the Content Switcher.

Method Effect
NDS.Tabs.init() Starts every .nds-tabs on the page that has not started yet
NDS.Tabs.create(element) Starts one tab set and returns its instance, or the instance it already has. Returns null when the tab set has no tab list, tabs or panels
NDS.Tabs.destroy(element) The same as instance.destroy()
instance.goTo(index) Opens a tab by its position, from 0
instance.getActiveTabIndex() Returns the position of the open tab
instance.getActiveTab(), instance.getActivePanel() Return the open tab and its panel
instance.destroy() Removes the listeners. The markup stays as it is
Event Fired on Detail
nds:tabs:change .nds-tabs, and it bubbles tabIndex, tab, panel, previousTab, previousPanel
var tabs = document.querySelector('#service-tabs'); tabs.addEventListener('nds:tabs:change', function (e) { console.log('Opened tab', e.detail.tabIndex); }); tabs.ndsTabs.goTo(2);

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

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