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
Apply for the service online in three steps.
A valid national ID and a recent photo.
The service fee is 100 riyals, paid online.
Call 19911 from Sunday to Thursday.
Variants
The first tab is open in the markup: its aria-selected is true, and every other panel has hidden. Keep that state in your markup, so the right panel shows before the script runs. To open another tab first, give that tab aria-selected="true" and tabindex="0", and remove hidden from its panel. Then set the first tab and panel like the others. The three Card rows of one option are one choice: add all three classes. The show-more button shows only when the tabs do not fit.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Standard (default) | — | — | Text-only tabs |
| Structure | With icons | canon #tabs-icons |
— | An icon before each label, for tabs that name distinct categories |
| Structure | Many tabs (hint: Scrolls when the tabs do not fit) | canon #tabs-many |
— | More tabs than fit in one row. The row scrolls. The show-more button moves it on by about one screen width, and back to the start at the end |
| Structure | Rail (hint: Only the icons show on a phone) (id: rail) | canon #tabs-rail |
— | Vertical tabs with an icon on each and a line beside them, for a short list beside content that needs the width, such as an inbox. Pick icons people know. On a phone the labels hide, the tabs turn square, and the panels get less padding at the sides. Screen readers still read each label. The open tab is filled |
| Layout | Horizontal (default) (not: rail) | — | — | Tabs in a row above the panels |
| Layout | Vertical (not: rail) | .nds-vertical |
.nds-tabs |
Tabs stacked beside the panels, for a long list or long labels. The open tab’s bar runs beside it, and the list stays in view while the page scrolls |
| Size | SM | .nds-sm |
.nds-tabs |
32px tabs with a small label |
| Size | MD (default) | — | — | 40px tabs. It needs no class |
| Size | LG | .nds-lg |
.nds-tabs |
48px tabs, for a larger touch target |
| Card | None (default) | — | — | No container |
| Card | Whole set (hint: Tabs and panels in one card) | .nds-card |
.nds-tabs |
The tabs and the panels in one card, for a page area that already has a background |
| Card | Whole set (hint: Tabs and panels in one card) | .nds-stroke |
.nds-tabs |
The same: gives the card its border |
| Card | Whole set (hint: Tabs and panels in one card) | .nds-shadow |
.nds-tabs |
The same: gives the card its shadow |
| Card | Panel only (hint: Only the panels in a card) | .nds-card |
.nds-tab-content |
Only the panels in a card, under a plain tab row |
| Card | Panel only (hint: Only the panels in a card) | .nds-stroke |
.nds-tab-content |
The same: gives the card its border |
| Card | Panel only (hint: Only the panels in a card) | .nds-shadow |
.nds-tab-content |
The same: gives the card its shadow |
| Divided | Divided (hint: A line between the tabs and the panels) (not: rail) | .nds-divided |
.nds-tabs |
A line between the tabs and the panels: under the row, or beside a vertical list |
| Divided | Lined (hint: A line under the tabs only, as wide as the tabs) (limit: 1 row width) | .nds-lined |
.nds-tabs:not(.nds-vertical) |
Horizontal only. A line under the tab row that ends after the last tab, instead of a divider across the full width. The row still scrolls when the tabs do not fit. Not with Center |
| Center | Center (limit: 1 row width) | .nds-center |
.nds-tab-list:not(.nds-vertical .nds-tab-list) |
Horizontal only. Centers a horizontal tab row while every tab fits. A row that scrolls starts at the edge |
| Loading | Loading | .nds-loading |
.nds-tabs |
Gray bars in place of the labels and the panel content while the data loads. On .nds-tab-list it bars the labels only |
Built-in Features
-
Auto-initialization
Every
.nds-tabson 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-verticalstacks 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
hiddenstay 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:changewith the new and the previous tab and panel. -
Programmatic Control
Open a tab, read the open panel or remove the behavior through
NDS.Tabsor the element'sndsTabsproperty.
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.hashonnds:tabs:change, and onhashchangeopen the matching tab withel.ndsTabs.goTo(index). Each switch is then a history entry, so Back opens the tab before it.history.replaceStatedrops the entry, and Back leaves the page. - Give the tab list an
aria-label, and pair each tab and panel witharia-controlsandaria-labelledby. - Build a long panel from
nds-blockparts withnds-block-titleheadings, 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-inlineto 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-inlinelines the content up with the tab row,nds-flush-blockremoves the space above and below, andnds-flushremoves 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.
| Token | Preview | Value |
|---|---|---|
--text-defaultRead by color | var(--colors-base-black)Dark mode var(--colors-neutral-200) | |
--text-primary-paragraphRead by color | var(--colors-neutral-700)Dark mode var(--colors-neutral-300) | |
--text-default-disabledRead by color | var(--colors-neutral-400) | |
--border-primaryRead by border, border-bottom | var(--colors-primary-600) | |
--divider-colorRead by background-color, --scroll-divider | var(--colors-alpha-black-10)Dark mode var(--colors-alpha-white-10) | |
--button-indicator-markRead 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 |
The full API is in the banner of _js/nds-tabs.js.