# Sliding Underline Tabs

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

- Category: [CSS Navigation](https://copy-css.com/category/navigation)
- Tags: tabs, animated, indicator, radio, has
- Live preview: https://copy-css.com/snippets/sliding-underline-tabs
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```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

```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; }
}
```

## Notes

- 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.
- the :has() selector: Chrome 105+, Safari 15.4+, Firefox 121+.
- Customize with: `--i`.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
