Skip to main contentSkip to docs navigation

Chassis

Switch between panes of content from a nav or a list with the Tab plugin, which keeps the ARIA roles and states of the tabs in sync.

@layer components
Requires JavaScript
Depends onNav
Context tokens

Introduction

The Tab plugin turns a nav, such as .nav-tabs or .nav-segments, or a list into a tabbed interface. Each control with data-cx-toggle="tab" shows the .tab-pane it targets and hides the pane of the control that was active before.

The .tab-content wrapper hides every .tab-pane child except the one with .active. The plugin moves .active between the controls and their panes, updates aria-selected and tabindex on the controls, and handles the arrow keys.

Basic structure

Add data-cx-toggle="tab" to each control and point it at a pane with data-cx-target. Wrap the panes in .tab-content, give each .tab-pane the id its control targets, and mark the initial control and its pane with .active.

This is some placeholder content the Home tab's associated content.

This is some placeholder content the Profile tab's associated content.

This is some placeholder content the Contact tab's associated content.

This is some placeholder content the Disabled tab's associated content.

HTML
<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item" role="none">
    <button class="nav-link active" id="home-tab" data-cx-toggle="tab" data-cx-target="#home-tab-pane" type="button" role="tab" aria-controls="home-tab-pane" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="none">
    <button class="nav-link" id="profile-tab" data-cx-toggle="tab" data-cx-target="#profile-tab-pane" type="button" role="tab" aria-controls="profile-tab-pane" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="none">
    <button class="nav-link" id="contact-tab" data-cx-toggle="tab" data-cx-target="#contact-tab-pane" type="button" role="tab" aria-controls="contact-tab-pane" aria-selected="false">Contact</button>
  </li>
  <li class="nav-item" role="none">
    <button class="nav-link" id="disabled-tab" data-cx-toggle="tab" data-cx-target="#disabled-tab-pane" type="button" role="tab" aria-controls="disabled-tab-pane" aria-selected="false" disabled>Disabled</button>
  </li>
</ul>
<div class="tab-content" id="myTabContent">
  <div class="tab-pane fade show active" id="home-tab-pane" role="tabpanel" aria-labelledby="home-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Home tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="profile-tab-pane" role="tabpanel" aria-labelledby="profile-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Profile tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="contact-tab-pane" role="tabpanel" aria-labelledby="contact-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Contact tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="disabled-tab-pane" role="tabpanel" aria-labelledby="disabled-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Disabled tab's</strong> associated content.</p>
  </div>
</div>

Custom markup

The plugin does not require <ul> markup: any element with .nav or role="tablist" can hold the tabs. Do not add role="tablist" to a <nav> element, since that overrides its native role as a navigation landmark. Place the tab list on an inner element instead, a <div> in this example, and wrap the <nav> around it.

HTML
<nav aria-label="Tab navigation">
  <div class="nav nav-tabs" id="nav-tab2" role="tablist">
    <button class="nav-link active" id="nav-home-tab2" data-cx-toggle="tab" data-cx-target="#nav-home2" type="button" role="tab" aria-controls="nav-home2" aria-selected="true">Home</button>
    <button class="nav-link" id="nav-profile-tab2" data-cx-toggle="tab" data-cx-target="#nav-profile2" type="button" role="tab" aria-controls="nav-profile2" aria-selected="false">Profile</button>
    <button class="nav-link" id="nav-contact-tab2" data-cx-toggle="tab" data-cx-target="#nav-contact2" type="button" role="tab" aria-controls="nav-contact2" aria-selected="false">Contact</button>
    <button class="nav-link" id="nav-disabled-tab2" data-cx-toggle="tab" data-cx-target="#nav-disabled2" type="button" role="tab" aria-controls="nav-disabled2" aria-selected="false" disabled>Disabled</button>
  </div>
</nav>
<div class="tab-content" id="nav-tabContent2">
  <div class="tab-pane fade show active" id="nav-home2" role="tabpanel" aria-labelledby="nav-home-tab2" tabindex="0">
    <p>This is some placeholder content the <strong>Home tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="nav-profile2" role="tabpanel" aria-labelledby="nav-profile-tab2" tabindex="0">
    <p>This is some placeholder content the <strong>Profile tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="nav-contact2" role="tabpanel" aria-labelledby="nav-contact-tab2" tabindex="0">
    <p>This is some placeholder content the <strong>Contact tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="nav-disabled2" role="tabpanel" aria-labelledby="nav-disabled-tab2" tabindex="0">
    <p>This is some placeholder content the <strong>Disabled tab's</strong> associated content.</p>
  </div>
</div>

Segments

The plugin works the same way with the .nav-segments variant.

This is some placeholder content the Home tab's associated content.

This is some placeholder content the Profile tab's associated content.

This is some placeholder content the Contact tab's associated content.

This is some placeholder content the Disabled tab's associated content.

HTML
<ul class="nav nav-segments" id="segments-tab" role="tablist">
  <li class="nav-item" role="none">
    <button class="nav-link active" id="segments-home-tab" data-cx-toggle="tab" data-cx-target="#segments-home" type="button" role="tab" aria-controls="segments-home" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="none">
    <button class="nav-link" id="segments-profile-tab" data-cx-toggle="tab" data-cx-target="#segments-profile" type="button" role="tab" aria-controls="segments-profile" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="none">
    <button class="nav-link" id="segments-contact-tab" data-cx-toggle="tab" data-cx-target="#segments-contact" type="button" role="tab" aria-controls="segments-contact" aria-selected="false">Contact</button>
  </li>
  <li class="nav-item" role="none">
    <button class="nav-link" id="segments-disabled-tab" data-cx-toggle="tab" data-cx-target="#segments-disabled" type="button" role="tab" aria-controls="segments-disabled" aria-selected="false" disabled>Disabled</button>
  </li>
</ul>
<div class="tab-content" id="segments-tabContent">
  <div class="tab-pane fade show active" id="segments-home" role="tabpanel" aria-labelledby="segments-home-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Home tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="segments-profile" role="tabpanel" aria-labelledby="segments-profile-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Profile tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="segments-contact" role="tabpanel" aria-labelledby="segments-contact-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Contact tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="segments-disabled" role="tabpanel" aria-labelledby="segments-disabled-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Disabled tab's</strong> associated content.</p>
  </div>
</div>

Add .flex-column to stack the segments vertically, and aria-orientation="vertical" to the tab list so assistive technologies announce the orientation.

This is some placeholder content the Home tab's associated content.

This is some placeholder content the Profile tab's associated content.

This is some placeholder content the Disabled tab's associated content.

This is some placeholder content the Messages tab's associated content.

This is some placeholder content the Settings tab's associated content.

HTML
<div class="d-flex align-items-start">
<div class="nav nav-segments flex-column me-md" id="v-segments-tab" role="tablist" aria-orientation="vertical">
  <button class="nav-link active" id="v-segments-home-tab" data-cx-toggle="tab" data-cx-target="#v-segments-home" type="button" role="tab" aria-controls="v-segments-home" aria-selected="true">Home</button>
  <button class="nav-link" id="v-segments-profile-tab" data-cx-toggle="tab" data-cx-target="#v-segments-profile" type="button" role="tab" aria-controls="v-segments-profile" aria-selected="false">Profile</button>
  <button class="nav-link" id="v-segments-disabled-tab" data-cx-toggle="tab" data-cx-target="#v-segments-disabled" type="button" role="tab" aria-controls="v-segments-disabled" aria-selected="false" disabled>Disabled</button>
  <button class="nav-link" id="v-segments-messages-tab" data-cx-toggle="tab" data-cx-target="#v-segments-messages" type="button" role="tab" aria-controls="v-segments-messages" aria-selected="false">Messages</button>
  <button class="nav-link" id="v-segments-settings-tab" data-cx-toggle="tab" data-cx-target="#v-segments-settings" type="button" role="tab" aria-controls="v-segments-settings" aria-selected="false">Settings</button>
</div>
<div class="tab-content" id="v-segments-tabContent">
  <div class="tab-pane fade show active" id="v-segments-home" role="tabpanel" aria-labelledby="v-segments-home-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Home tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="v-segments-profile" role="tabpanel" aria-labelledby="v-segments-profile-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Profile tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="v-segments-disabled" role="tabpanel" aria-labelledby="v-segments-disabled-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Disabled tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="v-segments-messages" role="tabpanel" aria-labelledby="v-segments-messages-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Messages tab's</strong> associated content.</p>
  </div>
  <div class="tab-pane fade" id="v-segments-settings" role="tabpanel" aria-labelledby="v-segments-settings-tab" tabindex="0">
    <p>This is some placeholder content the <strong>Settings tab's</strong> associated content.</p>
  </div>
</div>
</div>

Lists

The plugin also works with lists. Add data-cx-toggle="tab" to each .list-item and pair it with a .tab-pane; an anchor targets its pane with href. The plugin finds the tab list by its .list class, as it finds a .nav.

Some placeholder content in a paragraph relating to "Home".

Some placeholder content in a paragraph relating to "Profile".

Some placeholder content in a paragraph relating to "Messages".

Some placeholder content in a paragraph relating to "Settings".

HTML
<div class="grid">
<div class="col-span-4">
  <div class="list" id="list-tab" role="tablist">
    <a class="list-item list-action active" id="list-home-list" data-cx-toggle="tab" href="#list-home" role="tab" aria-controls="list-home">Home</a>
    <a class="list-item list-action" id="list-profile-list" data-cx-toggle="tab" href="#list-profile" role="tab" aria-controls="list-profile">Profile</a>
    <a class="list-item list-action" id="list-messages-list" data-cx-toggle="tab" href="#list-messages" role="tab" aria-controls="list-messages">Messages</a>
    <a class="list-item list-action" id="list-settings-list" data-cx-toggle="tab" href="#list-settings" role="tab" aria-controls="list-settings">Settings</a>
  </div>
</div>
<div class="col-span-8">
  <div class="tab-content" id="list-tabContent">
    <div class="tab-pane fade show active" id="list-home" role="tabpanel" aria-labelledby="list-home-list">
      <p>Some placeholder content in a paragraph relating to "Home".</p>
    </div>
    <div class="tab-pane fade" id="list-profile" role="tabpanel" aria-labelledby="list-profile-list">
      <p>Some placeholder content in a paragraph relating to "Profile".</p>
    </div>
    <div class="tab-pane fade" id="list-messages" role="tabpanel" aria-labelledby="list-messages-list">
      <p>Some placeholder content in a paragraph relating to "Messages".</p>
    </div>
    <div class="tab-pane fade" id="list-settings" role="tabpanel" aria-labelledby="list-settings-list">
      <p>Some placeholder content in a paragraph relating to "Settings".</p>
    </div>
  </div>
</div>
</div>

Fade transition

Add .fade to each .tab-pane to fade the panes in. The initially active pane also needs .show, which makes its content visible when the page loads.

HTML
<div class="tab-content">
  <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab" tabindex="0">...</div>
  <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab" tabindex="0">...</div>
  <div class="tab-pane fade" id="messages" role="tabpanel" aria-labelledby="messages-tab" tabindex="0">...</div>
  <div class="tab-pane fade" id="settings" role="tabpanel" aria-labelledby="settings-tab" tabindex="0">...</div>
</div>

Accessibility

Tabbed interfaces follow the ARIA Authoring Practices Guide tabs pattern: role="tablist" on the container, role="tab" and aria-selected on each control, and role="tabpanel" on each pane convey the structure and the current state to assistive technologies. Use <button> elements for the tabs, since they trigger a change in the page rather than navigate to another one.

ARIA attributes

When a tab is initialized, the plugin adds the roles that are missing from the markup: role="tablist" on the container, role="tab" on each control, role="none" on the .nav-item or .list-item around a control, and role="tabpanel" on each pane, with aria-labelledby when the control has an id. It sets aria-selected on every control and keeps it in sync as the active tab changes.

The plugin does not set aria-controls. Write it in the markup, together with the roles, so the attributes are present before the plugin runs.

Keyboard interaction

Only the active tab is in the tab order: the plugin sets tabindex="-1" on the inactive tabs and updates this roving tabindex as the active tab changes. With focus on a tab, the arrow keys activate the previous or next tab and wrap around at the ends, and Home and End activate the first and last tab. Disabled tabs are skipped.

The plugin does not distinguish horizontal from vertical tab lists: the up and left arrow keys both go to the previous tab, and the down and right arrow keys both go to the next one.

Make each tab panel focusable with tabindex="0", unless the first element with meaningful content inside the panel is already focusable. The plugin does not add this attribute.

The Tab plugin does not support tabbed interfaces that contain menus. The trigger of the displayed tab is not visible while it sits inside a closed menu, and no standard WAI-ARIA pattern maps this structure for users of assistive technologies.

JavaScript API

The Tab plugin activates tabs, shows their panes, and fires events around each change. Import the Tab class from the Chassis module:

JavaScript
import { Tab } from '@chassis-ui/css'

Triggers

The data-attribute API activates a tab on click without any JavaScript on the page beyond the bundled plugin. Add data-cx-toggle="tab" to each control:

AttributeDescription
data-cx-toggle="tab"Activates tab navigation on the control.
data-cx-targetCSS selector of the .tab-pane to show. An anchor can use href instead.
HTML
<!-- Nav tabs -->
<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item" role="none">
    <button class="nav-link active" id="home-tab" data-cx-toggle="tab" data-cx-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="none">
    <button class="nav-link" id="profile-tab" data-cx-toggle="tab" data-cx-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="none">
    <button class="nav-link" id="messages-tab" data-cx-toggle="tab" data-cx-target="#messages" type="button" role="tab" aria-controls="messages" aria-selected="false">Messages</button>
  </li>
  <li class="nav-item" role="none">
    <button class="nav-link" id="settings-tab" data-cx-toggle="tab" data-cx-target="#settings" type="button" role="tab" aria-controls="settings" aria-selected="false">Settings</button>
  </li>
</ul>

<!-- Tab panes -->
<div class="tab-content">
  <div class="tab-pane active" id="home" role="tabpanel" aria-labelledby="home-tab" tabindex="0">...</div>
  <div class="tab-pane" id="profile" role="tabpanel" aria-labelledby="profile-tab" tabindex="0">...</div>
  <div class="tab-pane" id="messages" role="tabpanel" aria-labelledby="messages-tab" tabindex="0">...</div>
  <div class="tab-pane" id="settings" role="tabpanel" aria-labelledby="settings-tab" tabindex="0">...</div>
</div>

Initialization

Each control is its own instance, and the plugin takes no options. For controls without data-cx-toggle="tab", create an instance per control with the Tab class and call show() on click:

JavaScript
const triggerTabList = document.querySelectorAll('#myTab button')
triggerTabList.forEach(triggerEl => {
  const tabTrigger = new Tab(triggerEl)

  triggerEl.addEventListener('click', event => {
    event.preventDefault()
    tabTrigger.show()
  })
})

Activate a single tab through its instance:

JavaScript
const triggerEl = document.querySelector('#myTab button[data-cx-target="#profile"]')
Tab.getOrCreateInstance(triggerEl).show() // Select tab by target

const triggerFirstTabEl = document.querySelector('#myTab li:first-child button')
Tab.getOrCreateInstance(triggerFirstTabEl).show() // Select first tab

Methods

The plugin exposes two instance methods and two static lookup methods for programmatic control:

All Chassis CSS component methods are asynchronous and initiate CSS transitions. Methods return immediately when the transition begins, not when it completes. Calling methods on components that are already transitioning will be ignored to prevent conflicts. Learn more about Chassis JavaScript patterns.

MethodDescription
disposeDestroys the instance and removes stored data from the element.
getInstanceStatic — returns the existing instance bound to a DOM element, or null. Example: Tab.getInstance(element)
getOrCreateInstanceStatic — returns the existing instance or creates one. Example: Tab.getOrCreateInstance(element)
showActivates the tab and shows its pane. The tab that was active before is deactivated and its pane is hidden. Returns before the pane is shown; listen for shown.cx.tab for completion.

Events

Showing a tab fires four events, in this order:

  1. hide.cx.tab (on the current active tab)
  2. show.cx.tab (on the tab to be shown)
  3. hidden.cx.tab (on the previous active tab, the same one as for the hide.cx.tab event)
  4. shown.cx.tab (on the newly active tab, the same one as for the show.cx.tab event)

When no tab is active yet, hide.cx.tab and hidden.cx.tab do not fire. Calling event.preventDefault() on hide.cx.tab or show.cx.tab cancels the change.

EventDescription
hide.cx.tabFired when a new tab is about to be shown, before the current active tab is hidden. event.target is the current active tab and event.relatedTarget the tab to be shown.
hidden.cx.tabFired after the previous active tab is hidden. event.target is the previous active tab and event.relatedTarget the new active tab.
show.cx.tabFired when a tab is about to be shown. event.target is the tab to be shown and event.relatedTarget the current active tab, if there is one.
shown.cx.tabFired after a tab is shown. event.target is the new active tab and event.relatedTarget the previous active tab, if there is one.
JavaScript
const tabEl = document.querySelector('button[data-cx-toggle="tab"]')
tabEl.addEventListener('shown.cx.tab', event => {
  event.target // newly activated tab
  event.relatedTarget // previous active tab
})