An underline glides under whichever item you hover, trailed by a slower faint echo, then settles back under the current page. Two pseudo-elements share one :has() driven offset.
Also known as: sliding underline navbar, moving underline indicator, nav indicator animation css.
<nav class="hfx-nav-glide-underline"> <a href="#" aria-current="page">Home</a> <a href="#">Work</a> <a href="#">About</a> <a href="#">Contact</a> </nav>
.hfx-nav-glide-underline {
--c: var(--hfx-color, #3b82f6);
--pos: 0;
position: relative;
display: inline-grid;
grid-auto-flow: column;
grid-auto-columns: 1fr;
font: 600 var(--hfx-size, 16px)/1.2 system-ui, sans-serif;
}
.hfx-nav-glide-underline::before,
.hfx-nav-glide-underline::after {
content: "";
position: absolute;
left: 0.8em;
bottom: 0;
width: calc(25% - 1.6em);
height: 0.16em;
border-radius: 1em;
transform: translateX(calc(var(--pos) * (100% + 1.6em)));
}
.hfx-nav-glide-underline::before {
background: color-mix(in srgb, var(--c) 35%, transparent);
transition: transform 0.6s cubic-bezier(0.65, 0, 0.35, 1);
}
.hfx-nav-glide-underline::after {
background: var(--c);
transition: transform 0.4s cubic-bezier(0.34, 1.3, 0.64, 1);
}
.hfx-nav-glide-underline a {
padding: 0.6em 1em 0.8em;
color: inherit;
text-align: center;
text-decoration: none;
transition: color 0.25s ease;
}
.hfx-nav-glide-underline a[aria-current="page"],
.hfx-nav-glide-underline a:is(:hover, :focus-visible) { color: var(--c); }
.hfx-nav-glide-underline:has(a:is(:hover, :focus-visible)) a[aria-current="page"]:not(:hover, :focus-visible) { color: inherit; }
.hfx-nav-glide-underline:has(a:nth-child(1):is(:hover, :focus-visible)) { --pos: 0; }
.hfx-nav-glide-underline:has(a:nth-child(2):is(:hover, :focus-visible)) { --pos: 1; }
.hfx-nav-glide-underline:has(a:nth-child(3):is(:hover, :focus-visible)) { --pos: 2; }
.hfx-nav-glide-underline:has(a:nth-child(4):is(:hover, :focus-visible)) { --pos: 3; }
@media (prefers-reduced-motion: reduce) {
.hfx-nav-glide-underline, .hfx-nav-glide-underline *, .hfx-nav-glide-underline::before, .hfx-nav-glide-underline::after, .hfx-nav-glide-underline *::before, .hfx-nav-glide-underline *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--hfx-color on the element or a parent.
Currently previewing #3b82f6.--hfx-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 16px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.