Tabs

Tabs

Account

Make changes to your account here. Click save when you're done.

<div class="tabs w-full" id="demo-tabs-with-panels">
<nav role="tablist" aria-orientation="horizontal" class="w-full">
<button type="button" role="tab" id="demo-tabs-with-panels-tab-1" aria-controls="demo-tabs-with-panels-panel-1" aria-selected="true" tabindex="0">Account</button>
<button type="button" role="tab" id="demo-tabs-with-panels-tab-2" aria-controls="demo-tabs-with-panels-panel-2" aria-selected="false" tabindex="-1">Password</button>
</nav>
<div role="tabpanel" id="demo-tabs-with-panels-panel-1" aria-labelledby="demo-tabs-with-panels-tab-1" tabindex="-1" aria-selected="true">
<div class="card">
<header>
<h2>Account</h2>
<p>Make changes to your account here. Click save when you're done.</p>
</header>
<section>
<form class="grid gap-4">
<div role="group" class="field">
<label for="demo-tabs-account-name">Name</label>
<input type="text" id="demo-tabs-account-name" value="Pedro Duarte" />
</div>
<div role="group" class="field">
<label for="demo-tabs-account-username">Username</label>
<input type="text" id="demo-tabs-account-username" value="@peduarte" />
</div>
</form>
</section>
<footer>
<button type="button" class="btn">Save changes</button>
</footer>
</div>
</div>
<div role="tabpanel" id="demo-tabs-with-panels-panel-2" aria-labelledby="demo-tabs-with-panels-tab-2" tabindex="-1" aria-selected="false" hidden>
<div class="card">
<header>
<h2>Password</h2>
<p>Change your password here. After saving, you'll be logged out.</p>
</header>
<section>
<form class="grid gap-4">
<div role="group" class="field">
<label for="demo-tabs-password-current">Current password</label>
<input type="password" id="demo-tabs-password-current" />
</div>
<div role="group" class="field">
<label for="demo-tabs-password-new">New password</label>
<input type="password" id="demo-tabs-password-new" />
</div>
</form>
</section>
<footer>
<button type="button" class="btn">Save password</button>
</footer>
</div>
</div>
</div>

data-variant="line" on the tablist draws underlined tabs

Overview

Orders, revenue and returns for the chosen period.

<div class="tabs w-full" id="demo-tabs-line">
<nav role="tablist" aria-orientation="horizontal" data-variant="line">
<button type="button" role="tab" id="demo-tabs-line-tab-1" aria-controls="demo-tabs-line-panel-1" aria-selected="true">Overview</button>
<button type="button" role="tab" id="demo-tabs-line-tab-2" aria-controls="demo-tabs-line-panel-2">Analytics</button>
<button type="button" role="tab" id="demo-tabs-line-tab-3" aria-controls="demo-tabs-line-panel-3">Reports</button>
</nav>
<div role="tabpanel" id="demo-tabs-line-panel-1" aria-labelledby="demo-tabs-line-tab-1">
<div class="card"><header><h2>Overview</h2><p>Orders, revenue and returns for the chosen period.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-line-panel-2" aria-labelledby="demo-tabs-line-tab-2" hidden>
<div class="card"><header><h2>Analytics</h2><p>Where the store's visitors come from and what they buy.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-line-panel-3" aria-labelledby="demo-tabs-line-tab-3" hidden>
<div class="card"><header><h2>Reports</h2><p>Saved reports, ready to open or to export.</p></header></div>
</div>
</div>

data-size="sm" is as tall as a small button, for a card header

Product Content

Magic Sheets

<div class="card w-full">
<header>
<h2>Product Content</h2>
<div class="card-action tabs">
<nav role="tablist" data-size="sm" aria-orientation="horizontal" aria-label="Language">
<button type="button" role="tab" id="demo-tabs-sm-tab-1" aria-controls="demo-tabs-sm-panel-1" aria-selected="true">EN</button>
<button type="button" role="tab" id="demo-tabs-sm-tab-2" aria-controls="demo-tabs-sm-panel-2">FR</button>
</nav>
</div>
</header>
<section>
<p role="tabpanel" id="demo-tabs-sm-panel-1" aria-labelledby="demo-tabs-sm-tab-1">Magic Sheets</p>
<p role="tabpanel" id="demo-tabs-sm-panel-2" aria-labelledby="demo-tabs-sm-tab-2" hidden>Draps magiques</p>
</section>
</div>

aria-orientation="vertical" stacks the tabs beside the panels

General

The store's name, address and time zone.

<div class="tabs w-full" id="demo-tabs-vertical">
<nav role="tablist" aria-orientation="vertical">
<button type="button" role="tab" id="demo-tabs-vertical-tab-1" aria-controls="demo-tabs-vertical-panel-1" aria-selected="true">General</button>
<button type="button" role="tab" id="demo-tabs-vertical-tab-2" aria-controls="demo-tabs-vertical-panel-2">Checkout</button>
<button type="button" role="tab" id="demo-tabs-vertical-tab-3" aria-controls="demo-tabs-vertical-panel-3">Notifications</button>
</nav>
<div role="tabpanel" id="demo-tabs-vertical-panel-1" aria-labelledby="demo-tabs-vertical-tab-1">
<div class="card"><header><h2>General</h2><p>The store's name, address and time zone.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-vertical-panel-2" aria-labelledby="demo-tabs-vertical-tab-2" hidden>
<div class="card"><header><h2>Checkout</h2><p>What a customer is asked for when paying.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-vertical-panel-3" aria-labelledby="demo-tabs-vertical-tab-3" hidden>
<div class="card"><header><h2>Notifications</h2><p>The emails the store sends.</p></header></div>
</div>
</div>

Vertical, with underlined tabs

General

The store's name, address and time zone.

<div class="tabs w-full" id="demo-tabs-vertical-line">
<nav role="tablist" aria-orientation="vertical" data-variant="line">
<button type="button" role="tab" id="demo-tabs-vertical-line-tab-1" aria-controls="demo-tabs-vertical-line-panel-1" aria-selected="true">General</button>
<button type="button" role="tab" id="demo-tabs-vertical-line-tab-2" aria-controls="demo-tabs-vertical-line-panel-2">Checkout</button>
<button type="button" role="tab" id="demo-tabs-vertical-line-tab-3" aria-controls="demo-tabs-vertical-line-panel-3">Notifications</button>
</nav>
<div role="tabpanel" id="demo-tabs-vertical-line-panel-1" aria-labelledby="demo-tabs-vertical-line-tab-1">
<div class="card"><header><h2>General</h2><p>The store's name, address and time zone.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-vertical-line-panel-2" aria-labelledby="demo-tabs-vertical-line-tab-2" hidden>
<div class="card"><header><h2>Checkout</h2><p>What a customer is asked for when paying.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-vertical-line-panel-3" aria-labelledby="demo-tabs-vertical-line-tab-3" hidden>
<div class="card"><header><h2>Notifications</h2><p>The emails the store sends.</p></header></div>
</div>
</div>

With icons

Account

Your name, email and photo.

<div class="tabs w-full" id="demo-tabs-icons">
<nav role="tablist" aria-orientation="horizontal">
<button type="button" role="tab" id="demo-tabs-icons-tab-1" aria-controls="demo-tabs-icons-panel-1" aria-selected="true"><i class="icon icon-user" aria-hidden="true"></i>Account</button>
<button type="button" role="tab" id="demo-tabs-icons-tab-2" aria-controls="demo-tabs-icons-panel-2"><i class="icon icon-lock" aria-hidden="true"></i>Security</button>
<button type="button" role="tab" id="demo-tabs-icons-tab-3" aria-controls="demo-tabs-icons-panel-3"><i class="icon icon-credit-card" aria-hidden="true"></i>Billing</button>
</nav>
<div role="tabpanel" id="demo-tabs-icons-panel-1" aria-labelledby="demo-tabs-icons-tab-1">
<div class="card"><header><h2>Account</h2><p>Your name, email and photo.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-icons-panel-2" aria-labelledby="demo-tabs-icons-tab-2" hidden>
<div class="card"><header><h2>Security</h2><p>Your password and two-step sign in.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-icons-panel-3" aria-labelledby="demo-tabs-icons-tab-3" hidden>
<div class="card"><header><h2>Billing</h2><p>Your plan and invoices.</p></header></div>
</div>
</div>

A disabled tab cannot be selected, and the arrow keys skip it

Overview

Orders, revenue and returns for the chosen period.

<div class="tabs w-full" id="demo-tabs-disabled">
<nav role="tablist" aria-orientation="horizontal">
<button type="button" role="tab" id="demo-tabs-disabled-tab-1" aria-controls="demo-tabs-disabled-panel-1" aria-selected="true">Overview</button>
<button type="button" role="tab" id="demo-tabs-disabled-tab-2" aria-controls="demo-tabs-disabled-panel-2" disabled>Analytics</button>
<button type="button" role="tab" id="demo-tabs-disabled-tab-3" aria-controls="demo-tabs-disabled-panel-3">Reports</button>
</nav>
<div role="tabpanel" id="demo-tabs-disabled-panel-1" aria-labelledby="demo-tabs-disabled-tab-1">
<div class="card"><header><h2>Overview</h2><p>Orders, revenue and returns for the chosen period.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-disabled-panel-2" aria-labelledby="demo-tabs-disabled-tab-2" hidden>
<div class="card"><header><h2>Analytics</h2><p>Where the store's visitors come from and what they buy.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-disabled-panel-3" aria-labelledby="demo-tabs-disabled-tab-3" hidden>
<div class="card"><header><h2>Reports</h2><p>Saved reports, ready to open or to export.</p></header></div>
</div>
</div>

A tab can be a dropdown: choosing an entry selects the tab

Overview

Every customer in the chosen period.

<div class="tabs w-full" id="demo-tabs-dropdown">
<nav role="tablist" aria-orientation="horizontal">
<button type="button" role="tab" id="demo-tabs-dropdown-tab-1" aria-controls="demo-tabs-dropdown-panel-1" aria-selected="true">Overview</button>
<div class="dropdown">
<button type="button" role="tab" id="demo-tabs-dropdown-tab-2" aria-controls="demo-tabs-dropdown-panel-2" aria-haspopup="menu" aria-expanded="false">By UTM<i class="icon icon-chevron-down" aria-hidden="true"></i></button>
<div data-popover aria-hidden="true" class="w-44">
<div role="menu" aria-labelledby="demo-tabs-dropdown-tab-2">
<div role="menuitemradio" aria-checked="false">UTM Source</div>
<div role="menuitemradio" aria-checked="false">UTM Medium</div>
<div role="menuitemradio" aria-checked="false">UTM Campaign</div>
</div>
</div>
</div>
<button type="button" role="tab" id="demo-tabs-dropdown-tab-3" aria-controls="demo-tabs-dropdown-panel-3">By Product</button>
</nav>
<div role="tabpanel" id="demo-tabs-dropdown-panel-1" aria-labelledby="demo-tabs-dropdown-tab-1">
<div class="card"><header><h2>Overview</h2><p>Every customer in the chosen period.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-dropdown-panel-2" aria-labelledby="demo-tabs-dropdown-tab-2" hidden>
<div class="card"><header><h2>By UTM</h2><p>Customers grouped by the chosen UTM value.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-dropdown-panel-3" aria-labelledby="demo-tabs-dropdown-tab-3" hidden>
<div class="card"><header><h2>By Product</h2><p>Customers grouped by the product they bought.</p></header></div>
</div>
</div>

Tabs that go to another page

role="tab" is for panels in the page. Tabs that go to another page are links in a nav.tabs, and the server marks the current one with aria-current="page".

No script runs on them, so the browser's Back button shows the right tab.

nav.tabs holding links goes to another page; aria-current="page" marks the current one

data-variant="line" and data-size="sm" go on the nav

<div class="flex flex-col gap-6">
<nav class="tabs" data-variant="line" aria-label="Campaign">
<a href="#demo-tabs-nav-analytics" aria-current="page">Analytics</a>
<a href="#demo-tabs-nav-offers">Offers</a>
<a href="#demo-tabs-nav-settings">Settings</a>
</nav>
<nav class="tabs" data-size="sm" aria-label="Report">
<a href="#demo-tabs-nav-overview" aria-current="page">Overview</a>
<a href="#demo-tabs-nav-by-product">By Product</a>
<a href="#demo-tabs-nav-by-utm">By UTM</a>
</nav>
</div>

The page selects a tab with select()

Overview

Orders, revenue and returns for the chosen period.

<div class="flex flex-col gap-4">
<div class="tabs w-full" id="demo-tabs-script">
<nav role="tablist" aria-orientation="horizontal">
<button type="button" role="tab" id="demo-tabs-script-tab-1" aria-controls="demo-tabs-script-panel-1" aria-selected="true">Overview</button>
<button type="button" role="tab" id="demo-tabs-script-tab-2" aria-controls="demo-tabs-script-panel-2">Analytics</button>
<button type="button" role="tab" id="demo-tabs-script-tab-3" aria-controls="demo-tabs-script-panel-3">Reports</button>
</nav>
<div role="tabpanel" id="demo-tabs-script-panel-1" aria-labelledby="demo-tabs-script-tab-1">
<div class="card"><header><h2>Overview</h2><p>Orders, revenue and returns for the chosen period.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-script-panel-2" aria-labelledby="demo-tabs-script-tab-2" hidden>
<div class="card"><header><h2>Analytics</h2><p>Where the store's visitors come from and what they buy.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-script-panel-3" aria-labelledby="demo-tabs-script-tab-3" hidden>
<div class="card"><header><h2>Reports</h2><p>Saved reports, ready to open or to export.</p></header></div>
</div>
</div>
<button type="button" class="btn self-start" data-variant="outline" data-size="sm" onclick="document.getElementById('demo-tabs-script').select(document.getElementById('demo-tabs-script-tab-3'))">Show Reports</button>
</div>

An element with data-close inside a tab closes it, and so does Delete on the tab

Order 1042

Two items, paid, waiting to ship.

<div class="tabs w-full" id="demo-tabs-close">
<nav role="tablist" aria-orientation="horizontal" aria-label="Open orders">
<button type="button" role="tab" id="demo-tabs-close-tab-1" aria-controls="demo-tabs-close-panel-1" aria-selected="true">Order 1042<i class="icon icon-x" data-close aria-hidden="true"></i></button>
<button type="button" role="tab" id="demo-tabs-close-tab-2" aria-controls="demo-tabs-close-panel-2">Order 1043<i class="icon icon-x" data-close aria-hidden="true"></i></button>
<button type="button" role="tab" id="demo-tabs-close-tab-3" aria-controls="demo-tabs-close-panel-3">Order 1044<i class="icon icon-x" data-close aria-hidden="true"></i></button>
</nav>
<div role="tabpanel" id="demo-tabs-close-panel-1" aria-labelledby="demo-tabs-close-tab-1">
<div class="card"><header><h2>Order 1042</h2><p>Two items, paid, waiting to ship.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-close-panel-2" aria-labelledby="demo-tabs-close-tab-2" hidden>
<div class="card"><header><h2>Order 1043</h2><p>One item, shipped on Monday.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-close-panel-3" aria-labelledby="demo-tabs-close-tab-3" hidden>
<div class="card"><header><h2>Order 1044</h2><p>Three items, refunded in part.</p></header></div>
</div>
</div>

The page hears closetab first, and keeps the tab with preventDefault()

Saved draft

This tab closes.

Nothing closed yet.
<div class="flex flex-col gap-3">
<div class="tabs w-full" id="demo-tabs-keep">
<nav role="tablist" aria-orientation="horizontal" aria-label="Open drafts">
<button type="button" role="tab" id="demo-tabs-keep-tab-1" aria-controls="demo-tabs-keep-panel-1" aria-selected="true">Saved draft<i class="icon icon-x" data-close aria-hidden="true"></i></button>
<button type="button" role="tab" id="demo-tabs-keep-tab-2" aria-controls="demo-tabs-keep-panel-2" data-unsaved>Unsaved draft<i class="icon icon-x" data-close aria-hidden="true"></i></button>
</nav>
<div role="tabpanel" id="demo-tabs-keep-panel-1" aria-labelledby="demo-tabs-keep-tab-1">
<div class="card"><header><h2>Saved draft</h2><p>This tab closes.</p></header></div>
</div>
<div role="tabpanel" id="demo-tabs-keep-panel-2" aria-labelledby="demo-tabs-keep-tab-2" hidden>
<div class="card"><header><h2>Unsaved draft</h2><p>The page keeps this tab open.</p></header></div>
</div>
</div>
<pre class="bg-muted overflow-x-auto rounded-lg p-4 font-mono text-xs" id="demo-tabs-keep-result">Nothing closed yet.</pre>
</div>
<script>
document.getElementById("demo-tabs-keep").addEventListener("closetab", (event) => {
const kept = event.detail.tab.hasAttribute("data-unsaved");
if (kept) event.preventDefault();
document.getElementById("demo-tabs-keep-result").textContent = event.detail.tab.textContent.trim() + (kept ? " was kept open." : " was closed.");
});
</script>

API Reference

Attributes

Attribute On Values Default Description
aria-orientation the [role=tablist] "horizontal", "vertical" "horizontal" "vertical" stacks the tabs beside the panels; the up and down arrow keys then move between them.
data-variant the [role=tablist], or a nav.tabs "line" none Underlined tabs, in place of the row of pills.
data-size the [role=tablist], or a nav.tabs "sm" none A 28px row, the height of a small button. For tabs in a card header.
aria-controls a tab the id of its panel none Names the panel the tab shows. Required.
aria-selected a tab "true", "false" the first enabled tab The tab that shows first. The script keeps it up to date.
disabled, or aria-disabled="true" a tab present or absent absent The tab cannot be selected and the keyboard skips it.
hidden a panel present or absent set by the script Every panel but the selected tab's is hidden.
aria-haspopup a tab inside a .dropdown "menu" none Makes the tab a dropdown. A click opens its menu, and choosing an entry selects the tab.
data-close an element inside a tab present or absent absent Makes the tab one that can be closed. A click on the element closes the tab.
aria-current a link in a nav.tabs "page" none Marks the link to the page being shown. The server writes it; no script changes it.

Methods and properties

Member Type Description
select(tab) method Shows the panel of the given tab element. A disabled tab is ignored.
refresh() method Shows the tab marked aria-selected="true", or the first enabled one. Call it after the page has added or removed tabs.
close(tab) method Removes the tab and its panel. If the tab was selected, the tab after it is selected, or the one before it.

Events

Event On Detail Description
closetab the tab, and it bubbles { tab, panel } Sent before a tab closes. preventDefault() on it keeps the tab.

Keyboard

Key Description
Arrow Right, Arrow Left Select the next or the previous tab, going round at the ends. With vertical tabs, Arrow Down and Arrow Up do.
Home, End Select the first or the last tab.
Tab Moves past the tabs: only the selected tab is in the tab order.
Delete On a tab that can be closed, closes it and moves focus to the tab beside it.

Tabs send no events of their own. Listen for click on the tablist, or read aria-selected.

A dropdown tab does not work inside a scrolling area, which cuts its menu off.

A nav.tabs of links has no script: a .tabs is started only when it holds a [role=tablist].

Once the script has started it, the element carries data-tabs-initialized="true" and sends tsugi:initialized. The methods and properties above are on the element itself.