CSS Sliding Underline Tabs

Equal-width tabs with one underline that slides to the selected tab, driven by :has() and a custom property.

  • tabs
  • animated
  • indicator
  • radio
  • has
HTML
<div class="slide-tabs">
  <div class="slide-tabs-list" role="radiogroup" aria-label="Period">
    <label><input type="radio" name="slide-tabs" checked>Day</label>
    <label><input type="radio" name="slide-tabs">Week</label>
    <label><input type="radio" name="slide-tabs">Month</label>
  </div>
  <div class="slide-tabs-panels">
    <p>1,284 visits today</p>
    <p>8,930 visits this week</p>
    <p>36,512 visits this month</p>
  </div>
</div>
CSS
.slide-tabs {
  width: 250px;
  font: 14px/1.5 system-ui, sans-serif;
}

.slide-tabs-list {
  --i: 0;
  position: relative;
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  border-bottom: 1px solid #e5e7eb;
}

/* One indicator, moved by --i */
.slide-tabs-list::after {
  content: "";
  position: absolute;
  bottom: -1px;
  left: 0;
  width: calc(100% / 3);
  height: 2px;
  border-radius: 2px;
  background: #0284c7;
  transform: translateX(calc(var(--i) * 100%));
  transition: transform 0.3s cubic-bezier(0.3, 0.7, 0.4, 1);
}

.slide-tabs-list:has(label:nth-child(2) input:checked) {
  --i: 1;
}

.slide-tabs-list:has(label:nth-child(3) input:checked) {
  --i: 2;
}

.slide-tabs-list label {
  padding: 10px 0;
  color: #6b7280;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  transition: color 0.2s ease;
}

.slide-tabs-list input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.slide-tabs-list label:has(input:checked) {
  color: #0284c7;
}

.slide-tabs-list label:has(input:focus-visible) {
  border-radius: 6px;
  outline: 2px solid #0284c7;
  outline-offset: -2px;
}

.slide-tabs-panels p {
  display: none;
  margin: 0;
  padding: 14px 2px;
  color: #111827;
  font-size: 18px;
  font-weight: 700;
}

.slide-tabs:has(label:nth-child(1) input:checked) .slide-tabs-panels > :nth-child(1),
.slide-tabs:has(label:nth-child(2) input:checked) .slide-tabs-panels > :nth-child(2),
.slide-tabs:has(label:nth-child(3) input:checked) .slide-tabs-panels > :nth-child(3) {
  display: block;
}

@media (prefers-reduced-motion: reduce) {
  .slide-tabs-list::after { transition: none; }
}

About this CSS Sliding Underline Tabs

This CSS Sliding Underline Tabs is 79 lines of pure CSS (1.6 KB) with no JavaScript and no dependencies. It uses transitions, transforms, CSS Grid and the :has() selector. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.

CSS features used

Customize it

Change these custom properties to restyle the sliding underline tabs without touching the rest of the CSS:

PropertyDefault
--i0

How to use this sliding underline tabs

  1. Copy the HTML and paste it where you want the element to appear.
  2. Copy the CSS into your stylesheet, or inside a <style> tag.
  3. Keep the .slide-tabs class name, or rename it in both places. Then tweak colors, sizes and timing to match your design.
  4. Using React or Next.js? Change class to className in the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).

Building with an AI tool?

“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.

Feedback