All effectsClick & switch

Sliding Tab Indicator

滑动标签指示条

A single underline slides and resizes to track the active tab.

Switch between the tabs

Where to use it

Make the selected state easy to track in tabs or segmented controls. Use one shared underline when tabs vary in width but control the same content area.

Practical example

An invoice screen puts All, Awaiting payment, and Paid under one header. Selecting Awaiting payment slides the line from All and stretches it to fit the longer label as the table changes.

AI prompt

Add Sliding Tab Indicator to [target tabs]. Move one underline to the selected tab’s position and width over about 220ms and recalculate after resizing. Preserve tab semantics, keyboard navigation, visibility of focus, and selected state. Snap the underline into position when reduced motion is preferred.

Example code

Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.

View HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Sliding Tab Indicator</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .tabs-demo { display: grid; gap: 8px; width: min(100%, 228px); }
    .tab-track {
      --tab-index: 0;
      position: relative;
      isolation: isolate;
      display: grid;
      grid-template-columns: repeat(3, minmax(0, 1fr));
      padding: 3px;
      background: #2b223c;
      border-radius: 8px;
    }
    .active-tab-indicator {
      position: absolute;
      z-index: -1;
      inset: 3px auto 3px 3px;
      width: calc((100% - 6px) / 3);
      background: #d6c4ff;
      border-radius: 6px;
      transform: translateX(calc(var(--tab-index) * 100%));
      transition: transform 260ms cubic-bezier(.2, .75, .2, 1);
    }
    .period-tab {
      padding: 6px 3px;
      color: #e3d6f4;
      background: transparent;
      border: 0;
      border-radius: 6px;
      font: inherit;
      font-size: 10px;
      font-weight: 700;
      cursor: pointer;
    }
    .period-tab[aria-selected="true"] { color: #2d1c43; }
    .period-panel {
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 10px;
      height: 51px;
      padding: 7px 10px;
      color: #f4ebff;
      background: #332747;
      border: 1px solid #624e7a;
      border-radius: 8px;
      font-size: 17px;
    }
    .period-panel[hidden] { display: none; }
    .bar-group { display: flex; align-items: end; gap: 3px; height: 100%; }
    .bar-group i {
      width: 5px;
      height: var(--bar-height);
      background: #bc9deb;
      border-radius: 2px 2px 0 0;
    }
    @media (prefers-reduced-motion: reduce) {
      .active-tab-indicator { transition: none; }
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="tabs-demo">
      <div class="tab-track" role="tablist" aria-label="Visits by period">
        <span class="active-tab-indicator" aria-hidden="true"></span>
        <button class="period-tab" type="button" role="tab" id="period-0" aria-controls="period-panel-0" aria-selected="true" tabindex="0">Day</button><button class="period-tab" type="button" role="tab" id="period-1" aria-controls="period-panel-1" aria-selected="false" tabindex="-1">Week</button><button class="period-tab" type="button" role="tab" id="period-2" aria-controls="period-panel-2" aria-selected="false" tabindex="-1">Month</button>
      </div>
      
      <div class="period-panel" id="period-panel-0" role="tabpanel" aria-labelledby="period-0" tabindex="0">
        <strong>124</strong>
        <span class="bar-group" aria-hidden="true"><i style="--bar-height: 26%"></i><i style="--bar-height: 74%"></i><i style="--bar-height: 43%"></i><i style="--bar-height: 84%"></i><i style="--bar-height: 56%"></i><i style="--bar-height: 69%"></i></span>
      </div>
      <div class="period-panel" id="period-panel-1" role="tabpanel" aria-labelledby="period-1" tabindex="0" hidden>
        <strong>836</strong>
        <span class="bar-group" aria-hidden="true"><i style="--bar-height: 24%"></i><i style="--bar-height: 45%"></i><i style="--bar-height: 39%"></i><i style="--bar-height: 74%"></i><i style="--bar-height: 81%"></i><i style="--bar-height: 64%"></i></span>
      </div>
      <div class="period-panel" id="period-panel-2" role="tabpanel" aria-labelledby="period-2" tabindex="0" hidden>
        <strong>3,140</strong>
        <span class="bar-group" aria-hidden="true"><i style="--bar-height: 31%"></i><i style="--bar-height: 43%"></i><i style="--bar-height: 48%"></i><i style="--bar-height: 64%"></i><i style="--bar-height: 79%"></i><i style="--bar-height: 92%"></i></span>
      </div>
    </div>
  </main>
  <script>
    const track = document.querySelector(".tab-track");
    const tabs = Array.from(track.querySelectorAll('[role="tab"]'));
    const panels = document.querySelectorAll('[role="tabpanel"]');

    function selectPeriod(chosen) {
      track.style.setProperty("--tab-index", chosen);
      tabs.forEach((tab, index) => {
        const active = index === chosen;
        tab.setAttribute("aria-selected", String(active));
        tab.tabIndex = active ? 0 : -1;
        panels[index].hidden = !active;
      });
    }

    tabs.forEach((tab, index) => {
      tab.addEventListener("click", () => selectPeriod(index));
      tab.addEventListener("keydown", (event) => {
        const last = tabs.length - 1;
        const positions = {
          ArrowRight: index === last ? 0 : index + 1,
          ArrowLeft: index === 0 ? last : index - 1,
          Home: 0,
          End: last,
        };
        const next = positions[event.key];
        if (typeof next !== "number") return;
        event.preventDefault();
        selectPeriod(next);
        tabs[next].focus();
      });
    });
  </script>
</body>
</html>

Original example by UI Skills