# Sliding Hover Nav

> Nav links with one highlight that slides to whichever link you hover, using :has(). No JavaScript.

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

## HTML

```html
<nav class="slide-nav" aria-label="Main">
  <a href="#" aria-current="page">Home</a>
  <a href="#">Work</a>
  <a href="#">Blog</a>
  <a href="#">Shop</a>
</nav>
```

## CSS

```css
.slide-nav {
  --n: 4; /* number of links */
  --i: 0; /* index of the highlighted link */
  position: relative;
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  padding: 4px;
  border: 1px solid #e0e7ff;
  border-radius: 12px;
  font: 600 14px system-ui, sans-serif;
}

.slide-nav::before {
  content: "";
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 4px;
  width: calc((100% - 8px) / var(--n));
  border-radius: 8px;
  background: #e0e7ff;
  transform: translateX(calc(var(--i) * 100%));
  transition: transform 0.3s cubic-bezier(0.3, 0.7, 0.4, 1);
}

.slide-nav a {
  position: relative;
  padding: 9px 16px;
  color: #64748b;
  text-align: center;
  text-decoration: none;
  transition: color 0.2s ease;
}

.slide-nav a:hover,
.slide-nav a:focus-visible,
.slide-nav:not(:hover) a[aria-current] {
  color: #3730a3;
}

.slide-nav a:focus-visible {
  outline: 2px solid #6366f1;
  border-radius: 8px;
}

/* Rest on the current page... */
.slide-nav:has(> :nth-child(2)[aria-current]) { --i: 1; }
.slide-nav:has(> :nth-child(3)[aria-current]) { --i: 2; }
.slide-nav:has(> :nth-child(4)[aria-current]) { --i: 3; }

/* ...and follow the pointer or keyboard focus */
.slide-nav:has(> :nth-child(1):is(:hover, :focus-visible)) { --i: 0; }
.slide-nav:has(> :nth-child(2):is(:hover, :focus-visible)) { --i: 1; }
.slide-nav:has(> :nth-child(3):is(:hover, :focus-visible)) { --i: 2; }
.slide-nav:has(> :nth-child(4):is(:hover, :focus-visible)) { --i: 3; }
```

## Notes

- This CSS Sliding Hover Nav is 56 lines of pure CSS (1.4 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: `--n`, `--i`.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
