Underline Tabs
A keyboard-accessible tab strip with the active tab underlined.
6 frameworksIntermediate
Underline tabs that carry a count badge beside each label.
<!-- The count badge sits beside a visible label and is aria-hidden, so it stays out of the accessible name. -->
<div class="max-w-2xl" data-tabs-badges>
<div class="flex gap-6 overflow-x-auto border-b border-gray-200 dark:border-gray-800" role="tablist" aria-label="Conversations">
<button class="group relative inline-flex items-center gap-2 whitespace-nowrap px-0.5 py-3 text-sm font-medium text-gray-600 transition-colors after:absolute after:inset-x-0 after:-bottom-px after:h-0.5 after:bg-transparent hover:text-gray-900 focus-visible:rounded focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 motion-reduce:transition-none aria-selected:text-blue-600 aria-selected:after:bg-blue-600 dark:text-gray-400 dark:hover:text-gray-100 dark:focus-visible:ring-blue-400 dark:aria-selected:text-blue-400 dark:aria-selected:after:bg-blue-400" type="button" role="tab" id="bdg-inbox" aria-controls="bdg-panel-inbox" aria-selected="true" tabindex="0">
Inbox
<span class="inline-flex min-w-5 items-center justify-center rounded-full bg-gray-100 px-1.5 text-xs font-semibold text-gray-600 group-aria-selected:bg-blue-600 group-aria-selected:text-white dark:bg-gray-800 dark:text-gray-300 dark:group-aria-selected:bg-blue-500" aria-hidden="true">12</span>
</button>
<button class="group relative inline-flex items-center gap-2 whitespace-nowrap px-0.5 py-3 text-sm font-medium text-gray-600 transition-colors after:absolute after:inset-x-0 after:-bottom-px after:h-0.5 after:bg-transparent hover:text-gray-900 focus-visible:rounded focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 motion-reduce:transition-none aria-selected:text-blue-600 aria-selected:after:bg-blue-600 dark:text-gray-400 dark:hover:text-gray-100 dark:focus-visible:ring-blue-400 dark:aria-selected:text-blue-400 dark:aria-selected:after:bg-blue-400" type="button" role="tab" id="bdg-mentions" aria-controls="bdg-panel-mentions" aria-selected="false" tabindex="-1">
Mentions
<span class="inline-flex min-w-5 items-center justify-center rounded-full bg-gray-100 px-1.5 text-xs font-semibold text-gray-600 group-aria-selected:bg-blue-600 group-aria-selected:text-white dark:bg-gray-800 dark:text-gray-300 dark:group-aria-selected:bg-blue-500" aria-hidden="true">3</span>
</button>
</div>
<div class="pt-4 text-sm leading-relaxed text-gray-600 focus-visible:rounded-md focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 dark:text-gray-300 dark:focus-visible:ring-blue-400" role="tabpanel" id="bdg-panel-inbox" aria-labelledby="bdg-inbox" tabindex="0"><p>12 conversations need a first reply.</p></div>
<div class="pt-4 text-sm leading-relaxed text-gray-600 focus-visible:rounded-md focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 dark:text-gray-300 dark:focus-visible:ring-blue-400" role="tabpanel" id="bdg-panel-mentions" aria-labelledby="bdg-mentions" tabindex="0" hidden><p>3 threads mention you directly.</p></div>
</div>
<script>
(function () {
document.querySelectorAll('[data-tabs-badges]').forEach(function (root) {
var tabs = [].slice.call(root.querySelectorAll('[role="tab"]'));
function panelFor(t) { return document.getElementById(t.getAttribute('aria-controls')); }
function select(i, focus) {
tabs.forEach(function (t, n) {
var on = n === i;
t.setAttribute('aria-selected', String(on));
t.tabIndex = on ? 0 : -1;
var p = panelFor(t);
if (p) p.hidden = !on;
});
if (focus) tabs[i].focus();
}
tabs.forEach(function (t, i) {
t.addEventListener('click', function () { select(i, false); });
t.addEventListener('keydown', function (e) {
var n = -1;
if (e.key === 'ArrowRight') n = (i + 1) % tabs.length;
else if (e.key === 'ArrowLeft') n = (i - 1 + tabs.length) % tabs.length;
else if (e.key === 'Home') n = 0;
else if (e.key === 'End') n = tabs.length - 1;
else return;
e.preventDefault();
select(n, true);
});
});
});
})();
</script>| Prop | Type | Default | Description |
|---|---|---|---|
itemsrequired | TabItem[] | - | The array of items to render. |
defaultTabId | string | - | Id of the item expanded on mount. |
onSelect | (id: string) => void | - | Fired with the menu item the user chose. |
className | string | - | Additional classes merged onto the root element. |
The badge sits next to a visible label and is marked `aria-hidden`, so it stays out of the accessible name; when the count itself matters to a screen reader, fold it into an `aria-label` on the tab instead. The active badge inverts to the accent so it never competes with the underline.