CSS Sliding Hover Nav

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

  • nav
  • has
  • indicator
  • hover
  • slide
  • highlight
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
.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; }

About this CSS Sliding Hover Nav

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. 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 hover nav without touching the rest of the CSS:

PropertyDefault
--n4
--i0

How to use this sliding hover nav

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