Animated tabs with a sliding indicator

A pill that slides to the tab you pick, or an ink bar that runs under the words. Both work with a mouse, a finger and the arrow keys.

UI loop

Tabs with a sliding indicator

1,204

visits this week

Your name, photo and the email we write to.

Sundry Millsundrymill.com/library
Animated tabs with a sliding indicator

Everything in the preview is live. Try it.

The code, one by one

Pill tabs

A white pill slides under the chosen tab. Click a tab, or tab in and use the arrow keys.

HTML
<div class="tb tb-pill" data-tabs>
  <div class="tb-list" role="tablist" aria-label="Report period">
    <button class="tb-tab" type="button" role="tab" id="tbp-0" aria-controls="tbp-0-panel" aria-selected="true">Week</button>
    <button class="tb-tab" type="button" role="tab" id="tbp-1" aria-controls="tbp-1-panel" aria-selected="false" tabindex="-1">Month</button>
    <button class="tb-tab" type="button" role="tab" id="tbp-2" aria-controls="tbp-2-panel" aria-selected="false" tabindex="-1">Year</button>
    <span class="tb-ink" aria-hidden="true"></span>
  </div>
  <div class="tb-panel" role="tabpanel" id="tbp-0-panel" aria-labelledby="tbp-0" tabindex="0"><p class="tb-big">1,204</p><p class="tb-sub">visits this week</p></div>
  <div class="tb-panel" role="tabpanel" id="tbp-1-panel" aria-labelledby="tbp-1" tabindex="0" hidden><p class="tb-big">5,880</p><p class="tb-sub">visits this month</p></div>
  <div class="tb-panel" role="tabpanel" id="tbp-2-panel" aria-labelledby="tbp-2" tabindex="0" hidden><p class="tb-big">61,390</p><p class="tb-sub">visits this year</p></div>
</div>
CSS
.tb { width: min(100%, 300px); }
.tb-list {
  position: relative;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  padding: 4px;
  border-radius: 999px;
  background: #EAE4DA;
}
.tb-tab {
  position: relative;
  z-index: 1;
  min-height: 44px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: #3D4755;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: color .3s;
}
.tb-tab[aria-selected="true"] { color: #17202C; }
.tb-tab:focus-visible { outline: 3px solid #2F55D4; outline-offset: 1px; }
.tb-ink {
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 0;
  width: var(--w, 0);
  border-radius: 999px;
  background: #FFFFFF;
  box-shadow: 0 2px 6px rgba(23, 32, 44, .16);
  transform: translateX(var(--x, 0));
}
.tb.is-ready .tb-ink { transition: transform .3s cubic-bezier(.33, 1, .68, 1), width .3s cubic-bezier(.33, 1, .68, 1); }
.tb-panel { padding: 16px 6px 4px; }
.tb-panel:focus-visible { outline: 3px solid #2F55D4; outline-offset: 2px; border-radius: 8px; }
.tb-big { margin: 0; font-size: 2rem; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.tb-sub { margin: 4px 0 0; color: #3D4755; }
@media (prefers-reduced-motion: reduce) {
  .tb.is-ready .tb-ink { transition: none; }
}
JavaScript
// Tabs as the WAI-ARIA Authoring Practices describe them: Tab goes into the list once,
// the arrow keys move between tabs, Home and End jump to the ends, and the ink slides to the choice
document.querySelectorAll('[data-tabs]').forEach((root) => {
  const tabs = [...root.querySelectorAll('[role="tab"]')];
  const ink = root.querySelector('.tb-ink');
  const place = () => {
    const on = tabs.find((t) => t.getAttribute('aria-selected') === 'true');
    ink.style.setProperty('--x', on.offsetLeft + 'px');
    ink.style.setProperty('--w', on.offsetWidth + 'px');
  };
  const select = (tab, focus) => {
    for (const t of tabs) {
      const on = t === tab;
      t.setAttribute('aria-selected', String(on));
      t.tabIndex = on ? 0 : -1;
      document.getElementById(t.getAttribute('aria-controls')).hidden = !on;
    }
    if (focus) tab.focus();
    place();
  };
  tabs.forEach((t, i) => {
    t.addEventListener('click', () => select(t));
    t.addEventListener('keydown', (e) => {
      const n = tabs.length;
      const to = { ArrowRight: (i + 1) % n, ArrowLeft: (i - 1 + n) % n, Home: 0, End: n - 1 }[e.key];
      if (to === undefined) return;
      e.preventDefault();
      select(tabs[to], true);
    });
  });
  place();
  // fonts loading and the window resizing move the tabs, so the ink follows them
  new ResizeObserver(place).observe(root);
  requestAnimationFrame(() => root.classList.add('is-ready'));
});

Underline tabs

The same markup and script with a different look: an ink bar under the words. Add the pill CSS above first, then this.

HTML
<div class="tb tb-line" data-tabs>
  <div class="tb-list" role="tablist" aria-label="Account settings">
    <button class="tb-tab" type="button" role="tab" id="tbl-0" aria-controls="tbl-0-panel" aria-selected="true">Profile</button>
    <button class="tb-tab" type="button" role="tab" id="tbl-1" aria-controls="tbl-1-panel" aria-selected="false" tabindex="-1">Billing</button>
    <button class="tb-tab" type="button" role="tab" id="tbl-2" aria-controls="tbl-2-panel" aria-selected="false" tabindex="-1">Alerts</button>
    <span class="tb-ink" aria-hidden="true"></span>
  </div>
  <div class="tb-panel" role="tabpanel" id="tbl-0-panel" aria-labelledby="tbl-0" tabindex="0"><p class="tb-sub">Your name, photo and the email we write to.</p></div>
  <div class="tb-panel" role="tabpanel" id="tbl-1-panel" aria-labelledby="tbl-1" tabindex="0" hidden><p class="tb-sub">Your plan, your card and every receipt.</p></div>
  <div class="tb-panel" role="tabpanel" id="tbl-2-panel" aria-labelledby="tbl-2" tabindex="0" hidden><p class="tb-sub">What we tell you about, and how often.</p></div>
</div>
CSS
.tb-line .tb-list {
  padding: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: inset 0 -2px 0 #DDD5C8;
}
.tb-line .tb-tab { border-radius: 8px 8px 0 0; }
.tb-line .tb-ink {
  top: auto;
  bottom: 0;
  height: 3px;
  border-radius: 3px;
  background: #17202C;
  box-shadow: none;
}

How to use it

  • The markup follows the tabs pattern in the WAI-ARIA Authoring Practices: a tablist, tabs with aria-selected and aria-controls, and panels labelled by their tab.
  • Only the chosen tab sits in the Tab order (tabindex 0). The arrow keys move between tabs and pick them as they go, and Home and End jump to the first and the last.
  • The ink is one element moved with transform and resized with width, and a ResizeObserver puts it back under the tab when fonts load or the window changes size.
  • Each panel has tabindex="0" so keyboard users can reach a panel that holds no links. Drop it if your panels start with a link or a field.

When to use it

  • Two to five views of the same thing that people switch between: week, month and year on a chart, or profile, billing and alerts on a settings page.
  • Content short enough that one panel at a time is enough, and nobody needs to compare two panels side by side.
  • Not for moving between pages of a site. Tabs change what is shown in place; links that load a new page belong in a nav with ordinary links.

A worked example: a stats card with week, month and year

The pill tabs on a dashboard card, with each panel filled by a request the first time it is picked.

  1. Build all three tabs and panels in the HTML. Leave the Month and Year panels empty, with hidden on them.
  2. Watch aria-selected with a click and keydown listener of your own, or wrap select() in the script, and fetch the numbers the first time a panel is shown.
  3. Show the number with tabular figures (font-variant-numeric: tabular-nums), as the demo does, so 1,204 and 5,880 take the same width and the card does not wobble.
  4. The inactive labels are #3D4755 on the #EAE4DA track, 7.4:1, and the focus ring is #2F55D4, 4.9:1 against the track, so both stay clear on the beige.

Showing an interaction like this to a client or in a tutorial? Framok records your screen and zooms in on every click for you.

Source: W3C, Tabs Pattern in the WAI-ARIA Authoring Practices Guide.

Free to use in personal and commercial work. No credit needed, though a link back is always nice.

More from the library

See the whole library